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
#! /usr/env/path python | |
sampleStr = "aabbbccdeeefff" | |
#compress the string a2b3c2de3f3 | |
def compressString(sampleStr): | |
count = 0 | |
temp = sampleStr[0] | |
compressedString = "" | |
for i in range(len(sampleStr)): | |
if (sampleStr[i] == temp): | |
count += 1 | |
temp = sampleStr[i] | |
else: | |
if (count > 1): | |
compressedString = compressedString + temp + str(count) | |
else: | |
compressedString = compressedString + temp | |
temp = sampleStr[i] | |
count = 1 | |
if (count > 1): | |
compressedString = compressedString + temp + str(count) | |
else: | |
compressedString = compressedString + temp | |
return compressedString | |
print(compressString(sampleStr)) |
No comments:
Post a Comment