I was wondering what I can do to increase the performance of this program. The algorithm is pretty slow right now but I was wondering what I can do to increase the efficiency of this program. I used lists instead of strings to avoid immutable string concatenation.
This program should compress strings by the number of characters within the string
Example:
aaabbbcc --> a3b3c2
Code:
def compressBad(string):
string = list(string)
mystr = []
last = string[0]
count = 1
for x in xrange(1,len(string)):
if string[x] == last:
count += 1
else:
mystr.append(last)
mystr.append(str(count))
last = string[x]
count = 1
# add the last character
mystr.append(last)
mystr.append(str(count))
return ''.join(mystr)
print compressBad("abaabccc")
output:
a1b1a2b1c3