Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Created May 14, 2019 20:52
Show Gist options
  • Select an option

  • Save jatinsharrma/ca70db6e889a1c79a736618a7b44dfd4 to your computer and use it in GitHub Desktop.

Select an option

Save jatinsharrma/ca70db6e889a1c79a736618a7b44dfd4 to your computer and use it in GitHub Desktop.
Python function which performs the run length encoding for a given String and returns the run length encoded String.
def encode(message):
count = 0
chatacter = ''
previous_char = message[0]
result = ''
length = len(message)
i = 0
while (i != length ):
chatacter = message[i]
if previous_char == chatacter :
count = count + 1
else :
result = result + str(count) + previous_char
count = 1
#print(str(i) + str(chatacter) + str(previous_char) + " " + str(result))
previous_char = chatacter
i = i + 1
return result + str(count) + str(previous_char)
encoded_message=encode("ABBBBCCCCCCCCAB")
print(encoded_message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment