Skip to content

Instantly share code, notes, and snippets.

@TimothyBramlett
Created October 30, 2015 05:27
Show Gist options
  • Save TimothyBramlett/c721e78cadbb547c809a to your computer and use it in GitHub Desktop.
Save TimothyBramlett/c721e78cadbb547c809a to your computer and use it in GitHub Desktop.
def compress(stringy):
prvChr = ""
newStr = ""
matchCnt = 1
lenny = len(stringy)
for i in range(lenny):
# if not the last char in the string
if i != lenny-1:
# if current char equals next char
if stringy[i] == stringy[i + 1]:
matchCnt += 1
# else end of a matching sequence
else:
newStr += str(matchCnt)
newStr += stringy[i]
matchCnt=1
# the last char of the string
else:
newStr += str(matchCnt)
newStr += stringy[i]
matchCnt=1
return newStr
myString = "aaaaaaBBBB***aa"
print compress(myString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment