Created
October 30, 2015 05:27
-
-
Save TimothyBramlett/c721e78cadbb547c809a 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(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