Home > Mobile >  String Compression using python
String Compression using python

Time:06-03

Everything else seems to work just fine, but last character is always off by 1. For example, if I input abcccddd, I get a1b1c3d2 but I should get a1b1c3d3. Any hint would be much appreciated!

Prompt: String Compression: Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z). Do the easy thing first. Compress the string, then compare the lengths. Be careful that you aren't repeatedly concatenating strings together, this can be very inefficient.

def compression(string): 
    hash = {}
    list = []
    count = 0
    for i in range(len(string) - 1): 
        if string[i - 1] != string[i] or i == 0: 
            if string[i] != string[i   1] or i == len(string) - 2: 
                count = count   1
                list.append(str(string[i]))
                list.append(str(count))
                count = 0
            elif string[i] == string[i   1]: 
                count = count   1
        elif string[i - 1] == string[i]:
            if string[i] != string[i   1] or i == len(string) - 2: 
                count = count   1
                list.append(str(string[i]))
                list.append(str(count))
                count = 0
            if string[i] == string[i   1]: 
                count = count   1
        print(list)
    result =  "".join(list)
    if len(result) == len(string): 
        return string
    else: 
        return result
string = "abcccfffgggg"
compression(string)

CodePudding user response:

If you are up to the itertools module - try groupby:

s = 'bbbbaacddd' # dddeeef gg'
groups = [(label, len(list(group))) 
                  for label, group in groupby(s) if label] #

compressed = "".join("{}{}".format(label, count) for label, count in groups)

print(compressed)  #    b4a2c1d3         

Another way to achieve it, is to use more_itertools.run_length.


>>> compressed = list(run_length.encode(s))
>>> compressed
[('b', 4), ('a', 2), ('c', 1), ('d', 3)]
>>> ''.join("{}{}".format(label, count) for label, count in compressed)
'b4a2c1d3'

CodePudding user response:

You can make this easier by using a dictionary and deleting the characters whenever you use them, which counts the number of characters you want to compress

string = "aabccccaaaa"

output = ""
lastchar = string[0]
counts = {lastchar:1}

for i in range(1, len(string)):
    s = string[i]
    if s == lastchar:
        counts[s]  = 1
    else:
        output  = f"{lastchar}{counts[lastchar]}" if counts[lastchar] > 1 else lastchar
        del counts[lastchar]
        counts[s] = 1
    lastchar = s

print(output f"{lastchar}{counts[lastchar]}" if counts[lastchar] > 1 else lastchar)
  • Related