Count the number of max consecutive “a”'s from a string. Python 3

后端 未结 5 1144
清酒与你
清酒与你 2021-01-05 08:43

Say that the user inputs:

\"daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm\"

How would you go about finding the highest

5条回答
  •  -上瘾入骨i
    2021-01-05 09:06

    Try this:

    import collections
    
    def runLenEncode(s):
        start, i = 0, 0;
        answer = []
        while i=thresholds[char]:
                count = repLens[char]
            answer.append(char*count)
        return ''.join(answer)
    
    def maxFilter(encoding, repLens):
        maxCounts = collections.defaultdict(int)
        for char, count in encoding:
            if char in repLens and count > maxCounts[char]:
                maxCounts[char] = count
        maxCounts = dict(maxCounts)
        answer = []
        for char, count in encoding:
            if char in repLens and count==maxCounts[char]:
                count = repLens[char]
            answer.append(char*count)
        return ''.join(answer)
    
    if __name__ == "__main__":
        print('starting')
    
        s = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"
        encoding = runLenEncode(s)
        print("encoding:", encoding)
        thresholds = {'a':3}
        repLens = {'a':2}
        decoded = runLenFilter(encoding, thresholds, repLens)
        print('lenFilter:', decoded)
        filtered = maxFilter(encoding, repLens)
        print("maxFilter:", filtered)
    
        print('done')
    

    And the output:

    $ python3 myFile.py
    starting
    encoding: [('d', 1), ('a', 1), ('s', 1), ('l', 1), ('a', 1), ('k', 1), ('n', 1), ('d', 1), ('l', 1), ('a', 5), ('j', 1), ('n', 1), ('j', 1), ('n', 1), ('d', 1), ('i', 1), ('b', 1), ('n', 1), ('i', 1), ('a', 3), ('f', 1), ('i', 1), ('j', 1), ('d', 1), ('n', 1), ('f', 1), ('i', 1), ('j', 1), ('d', 1), ('n', 1), ('s', 1), ('i', 1), ('j', 1), ('f', 1), ('n', 1), ('s', 1), ('d', 1), ('i', 1), ('n', 1), ('i', 1), ('f', 1), ('a', 11), ('f', 1), ('n', 2), ('a', 1), ('s', 1), ('m', 1)]
    lenFilter: daslakndlaajnjndibniaafijdnfijdnsijfnsdinifaafnnasm
    maxFilter: daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaafnnasm
    done
    

提交回复
热议问题