Answers for "string compression python"

0

string compression python

def compress(string):
    index = 0
    compressed = ""
    len_str = len(string)
    while index != len_str:
        count = 1
        while (index < len_str-1) and (string[index] == string[index+1]):
            count = count + 1
            index = index + 1
        if count == 1:
            compressed += str(string[index])
        else:
            compressed += str(string[index]) + str(count)
        index = index + 1
    return compressed
       
 
string = "pythooonnnpool"
print(compress(string))
Posted by: Guest on August-27-2021

Code answers related to "string compression python"

Python Answers by Framework

Browse Popular Code Answers by Language