Created
May 14, 2019 20:52
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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