Need to write a python function which performs the run length encoding for a given string and returns the run length encoded String.
Went through various posts in Stack Overflow and other websites in Google to get a proper understanding in Run Length Encoding in Python. Tried coding and got some output but it doesn't match the specified output. I have stated my code and output below:
def encode(input_string):
characters = []
result = ''
for character in input_string:
# End loop if all characters were counted
if set(characters) == set(input_string):
break
if character not in characters:
characters.append(character)
count = input_string.count(character)
result += character
if count > 1:
result += str(count)
return result
#Provide different values for message and test your program
encoded_message=encode("ABBBBCCCCCCCCAB")
print(encoded_message)
For the above string, I am getting the output as : A2B5C8
But the expected output is :1A4B8C1A1B
It would be really great if someone tell me where to make changes in the code to get the expected format of output.