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

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

Say that the user inputs:

\"daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm\"

How would you go about finding the highest

5条回答
  •  逝去的感伤
    2021-01-05 09:31

    A lower level approach if you don't want to use regular expressions.

    def count_and_reduce(s, a):
        num = 0
        maxnum = 0
        out = ''
        for c in s:
            if c == a:
                num += 1
                maxnum = max(num, maxnum)
            else:
                num = 0
            if num <= 2:
                out += c
    
        return maxnum, out
    

提交回复
热议问题