Skip to content

Instantly share code, notes, and snippets.

@Pierian-Data
Created November 30, 2017 23:51
Show Gist options
  • Save Pierian-Data/4c85a5adc36a282223065d52cc178595 to your computer and use it in GitHub Desktop.
Save Pierian-Data/4c85a5adc36a282223065d52cc178595 to your computer and use it in GitHub Desktop.
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