Create a compress function in Python?

后端 未结 19 1026
感动是毒
感动是毒 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:22
    s=input("Enter the string:")
    temp={}
    result=" "
    for x in s:
        if x in temp:
            temp[x]=temp[x]+1
        else:
            temp[x]=1
    for key,value in temp.items():
        result+=str(key)+str(value)
    

    print(result)

    0 讨论(0)
  • 2021-01-05 04:23

    Use python's standard library re.

    def compress(string):
        import re
        p=r'(\w+?)\1+' # non greedy, group1 1
        sub_str=string
        for m in re.finditer(p,string):
            num=m[0].count(m[1])
            sub_str=re.sub(m[0],f'{m[1]}{num}',sub_str)
        return sub_str
    
    string='aaaaaaaabbbbbbbbbcccccccckkkkkkkkkkkppp'
    string2='ababcdcd'
    string3='abcdabcd' 
    string4='ababcdabcdefabcdcd' 
    
    print(compress(string))
    print(compress(string2))
    print(compress(string3))
    print(compress(string4))
    

    Resut:

    a8b9c8k11p3                                                                     
    ab2cd2                                                                          
    abcd2
    ab2cdabcdefabcd2 
    
    0 讨论(0)
  • 2021-01-05 04:24

    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 one
        for i in range(len(string)-1):
            if(string[i] == string[i+1]):
                count+=1
            else:
                if(count > 1):
                    #Ignore if no repeats
                    res += str(count)
                res += string[i+1]
                count = 1
        #print last one
        if(count > 1):
            res += str(count)
        return res
    

    Here are a few examples:

    >>> compress("ddaaaff")
    'd2a3f2'
    >>> compress("daaaafffyy")
    'da4f3y2'
    >>> compress("mississippi")
    'mis2is2ip2i'
    
    0 讨论(0)
  • 2021-01-05 04:25
    from collections import Counter
    def string_compression(string):
        counter = Counter(string)
        result = ''
        for k, v in counter.items():
            result = result + k + str(v)
        print(result)
    
    0 讨论(0)
  • 2021-01-05 04:30

    Here is a short python implementation of a compression function:

    #d=compress('xxcccdex')
    #print(d)
    
    def compress(word):
        list1=[]
        for i in range(len(word)):
            list1.append(word[i].lower())
        num=0
        dict1={}
        for i in range(len(list1)):
            if(list1[i] in list(dict1.keys())):
                dict1[list1[i]]=dict1[list1[i]]+1
            else:
                dict1[list1[i]]=1
    
        s=list(dict1.keys())
        v=list(dict1.values())
        word=''
        for i in range(len(s)):
            word=word+s[i]+str(v[i])
        return word
    
    0 讨论(0)
  • 2021-01-05 04:33

    For a coding interview, where it was about the algorithm, and not about my knowledge of Python, its internal representation of data structures, or the time complexity of operations such as string concatenation:

    def compress(message: str) -> str:
        output = ""
        length = 0
        previous: str = None
        for char in message:
            if previous is None or char == previous:
                length += 1
            else:
                output += previous
                if length > 1:
                    output += str(length)
                length = 1
            previous = char
        if previous is not None:
            output += previous
            if length > 1:
                output += str(length)
        return output
    

    For code I'd actually use in production, not reinventing any wheels, being more testable, using iterators until the last step for space efficiency, and using join() instead of string concatenation for time efficiency:

    from itertools import groupby
    from typing import Iterator
    
    
    def compressed_groups(message: str) -> Iterator[str]:
        for char, group in groupby(message):
            length = sum(1 for _ in group)
            yield char + (str(length) if length > 1 else "")
    
    
    def compress(message: str) -> str:
        return "".join(compressed_groups(message))
    

    Taking things a step further, for even more testability:

    from itertools import groupby
    from typing import Iterator
    from collections import namedtuple
    
    
    class Segment(namedtuple('Segment', ['char', 'length'])):
    
        def __str__(self) -> str:
            return self.char + (str(self.length) if self.length > 1 else "")
    
    
    def segments(message: str) -> Iterator[Segment]:
        for char, group in groupby(message):
            yield Segment(char, sum(1 for _ in group))
    
    
    def compress(message: str) -> str:
        return "".join(str(s) for s in segments(message))
    

    Going all-out and providing a Value Object CompressedString:

    from itertools import groupby
    from typing import Iterator
    from collections import namedtuple
    
    
    class Segment(namedtuple('Segment', ['char', 'length'])):
    
        def __str__(self) -> str:
            return self.char + (str(self.length) if self.length > 1 else "")
    
    
    class CompressedString(str):
    
        @classmethod
        def compress(cls, message: str) -> "CompressedString":
            return cls("".join(str(s) for s in cls._segments(message)))
    
        @staticmethod
        def _segments(message: str) -> Iterator[Segment]:
            for char, group in groupby(message):
                yield Segment(char, sum(1 for _ in group))
    
    
    def compress(message: str) -> str:
        return CompressedString.compress(message)
    
    0 讨论(0)
提交回复
热议问题