Create a compress function in Python?

后端 未结 19 1027
感动是毒
感动是毒 2021-01-05 03:37

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 vers

相关标签:
19条回答
  • 2021-01-05 04:34

    Short version with generators:

    from itertools import groupby
    import re
    def compress(string):
        return re.sub(r'(?<![0-9])[1](?![0-9])', '', ''.join('%s%s' % (char, sum(1 for _ 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

    0 讨论(0)
  • 2021-01-05 04:34
    x="mississippi"
    res = ""
    count = 0
    while (len(x) > 0):
        count = 1
        res= ""
        for j in range(1, len(x)):
            if x[0]==x[j]:
                count= count + 1
            else:
                res = res + x[j]
        print(x[0], count, end=" ")
        x=res
    
    0 讨论(0)
  • 2021-01-05 04:34

    You can simply achieve that by:

    gstr="aaabbccccffffdee"
    last=gstr[0]
    count=0
    rstr=""
    for i in gstr:
        if i==last:
            count=count+1
        elif i!=last:
            rstr=rstr+last+str(count)
            count=1
            last=i
    rstr=rstr+last+str(count)
    print ("Required string for given string {} after conversion is {}.".format(gstr,rstr))
    
    0 讨论(0)
  • 2021-01-05 04:35

    This is a solution to the problem. But keep in mind that this method only effectively works if there's a lot of repetition, specifically if consecutive characters are repetitive. Otherwise, it will only worsen the situation.

    e.g.,
    AABCD --> A2B1C1D1
    BcDG ---> B1c1D1G1

    def compress_string(s):
        result = [""] * len(s)
        visited = None
    
        index = 0
        count = 1
    
        for c in s:
            if c == visited:
                count += 1
                result[index] = f"{c}{count}"
            else:
                count = 1
                index += 1
                result[index] = f"{c}{count}"
                visited = c
    
        return "".join(result)
    
    0 讨论(0)
  • 2021-01-05 04:36

    string = 'aabccccd' output = '2a3b4c4d'

    new_string = " "
    count = 1
    for i in range(len(string)-1):
        if string[i] == string[i+1]:
            count = count + 1
        else:         
            new_string =  new_string + str(count) + string[i]
            count = 1 
    new_string = new_string + str(count) + string[i+1]    
    print(new_string)
    
    0 讨论(0)
  • 2021-01-05 04:36

    This Is The Modification Of Patrick Yu's Code. His Code Fails In The Below Test Cases.

    SAMPLE INPUT:
    c
    aaaaaaaaaabcdefgh

    EXPECTED OUTPUT:
    c1
    a10b1c1d1e1f1g1h1

    OUPUT OF Patrick's Code:
    c
    a10bcdefgh

    Below Is The Modification Of His Code:

    
    def Compress(S):
        Ans = S[0]
        count = 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                count += 1
            else:
                if count >= 1:
                    Ans += str(count)
                Ans += S[i+1]
                count = 1
        if count>=1:
            Ans += str(count)
        return Ans
    

    Just The Condition Must Be Changed From Greater(">") To Greater Than Equal To(">=") When Comparing The Count With 1.

    0 讨论(0)
提交回复
热议问题