"A book to train programming skills to fight in the world" Python code Solution example --1.6 String compression
def compressBad(input_str):
compressedString = ""
countConsecutive = 0
for i in range(len(input_str)):
countConsecutive = countConsecutive + 1
if i+1 >= len(input_str) or input_str[i] != input_str[i+1]:
compressedString = compressedString + input_str[i] + str(countConsecutive)
countConsecutive = 0
return compressedString if len(compressedString) < len(input_str) else input_str
input_str = "aabcccccaaa"
print(compressBad(input_str))