string compression python

Solutions on MaxInterview for string compression python by the best coders in the world

showing results for - "string compression python"
Carla
29 Oct 2019
1def compress(string):
2    index = 0
3    compressed = ""
4    len_str = len(string)
5    while index != len_str:
6        count = 1
7        while (index < len_str-1) and (string[index] == string[index+1]):
8            count = count + 1
9            index = index + 1
10        if count == 1:
11            compressed += str(string[index])
12        else:
13            compressed += str(string[index]) + str(count)
14        index = index + 1
15    return compressed
16       
17 
18string = "pythooonnnpool"
19print(compress(string))
20