Created
November 30, 2017 23:51
-
-
Save Pierian-Data/4c85a5adc36a282223065d52cc178595 to your computer and use it in GitHub Desktop.
This file contains 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 compress(s): | |
run = "" | |
length = len(s) | |
if length == 0: | |
return "" | |
# "A" --> "A1" | |
if length == 1: | |
return s + "1" | |
cnt = 1 | |
i = 1 | |
while i < length: | |
if s[i] == s[i-1]: | |
cnt +=1 | |
else: | |
run = run + s[i-1] + str(cnt) | |
cnt = 1 | |
i += 1 | |
run = run + s[i-1] + str(cnt) | |
return run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment