Strings, Loops
def compress(string):
"""Return a compressed version of the given string."""
compressed = []
current = ''
count = 0
for ch in string:
if ch != current:
compressed.append(current)
# Only append count if it is greater than 1
if count > 1:
compressed.append(str(int(count)))
# Reset `count` and update `current`
current = ch
count = 0
count += 1
# Append remaining character(s)
compressed.append(current)
if count > 1:
compressed.append(str(int(count)))
return ''.join(compressed)