Skip to content Skip to sidebar Skip to footer

Create A Compress Function In Python?

I need to create a function called compress that compresses a string by replacing any repeated letters with a letter and number. My function should return the shortened version of

Solution 1:

Here is a short python implementation of a compression function:

def compress(string):

    res = ""

    count = 1#Add in first character
    res += string[0]

    #Iterate through loop, skipping last onefor i inrange(len(string)-1):
        if(string[i] == string[i+1]):
            count+=1else:
            if(count > 1):
                #Ignore if no repeats
                res += str(count)
            res += string[i+1]
            count = 1#print last oneif(count > 1):
        res += str(count)
    return res

Here are a few examples:

>>> compress("ddaaaff")
'd2a3f2'>>> compress("daaaafffyy")
'da4f3y2'>>> compress("mississippi")
'mis2is2ip2i'

Solution 2:

Short version with generators:

from itertools import groupby
import re
defcompress(string):
    return re.sub(r'(?<![0-9])[1](?![0-9])', '', ''.join('%s%s' % (char, sum(1for _ in group)) for char, group in groupby(string)))

(1) Grouping by chars with groupby(string)

(2) Counting length of group with sum(1 for _ in group) (because no len on group is possible)

(3) Joining into proper format

(4) Removing 1 chars for single items when there is a no digit before and after 1

Solution 3:

There are several reasons why this doesn't work. You really need to try debugging this yourself first. Put in a few print statements to trace the execution. For instance:

defcompress(s):
    count=0for i inrange(0, len(s)):
        print"Checking character", i, s[i]
        if s[i] == s[i-1]:
            count += 1
        c = s.count(s[i])
        print"Found", s[i], c, "times"returnstr(s[i]) + str(c)

print compress("ddaaaff")

Here's the output:

Checking character 0 d
Found d 2 times
Checking character 1 d
Found d 2 times
Checking character 2 a
Found a 3 times
Checking character 3 a
Found a 3 times
Checking character 4 a
Found a 3 times
Checking character 5 f
Found f 2 times
Checking character 6 f
Found f 2 times
f2

Process finished with exit code 0

(1) You throw away the results of all but the last letter's search. (2) You count all occurrences, not merely the consecutive ones. (3) You cast a string to a string -- redundant.

Try working through this example with pencil and paper. Write down the steps you use, as a human being, to parse the string. Work on translating those to Python.

Solution 4:

x="mississippi"
res = ""
count = 0while (len(x) > 0):
    count = 1
    res= ""for j in range(1, len(x)):
        if x[0]==x[j]:
            count= count + 1else:
            res = res + x[j]
    print(x[0], count, end=" ")
    x=res

Solution 5:

Just another simplest way to perform this:

def compress(str1):
    output = ''
    initial = str1[0]
    output = output + initial
    count = 1for item in str1[1:]:
        if item == initial:
            count = count + 1else:
            if count == 1:
                count = ''output = output + str(count)
            count = 1
            initial = item
            output = output + item
    print (output)

Which gives the output as required, examples:

>> compress("aaaaaaaccddddeehhyiiiuuo")
a7c2d4e2h2yi3u2o

>> compress("lllhhjuuuirrdtt")
l3h2ju3ir2dt

>> compress("mississippi")
mis2is2ip2i

Post a Comment for "Create A Compress Function In Python?"