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

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

Say that the user inputs:

\"daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm\"

How would you go about finding the highest

5条回答
  •  庸人自扰
    2021-01-05 09:30

    The way I would do it.

    s = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"
    print(s)
    a_len = len(s)
    found_a_len = 0
    keep_going = True
    while a_len>0 and keep_going:
        aas = "a" * a_len
        if aas in s:
            found_a_len = a_len
            keep_going = False
        a_len=a_len -1
    print ("max length of a:" , found_a_len)
    keep_going = True
    while keep_going:
        s=s.replace("aaa","aa")
        if "aaa" not in s:
            keep_going = False
    print(s)
    

    this outputs:

    daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm
    max length of a: 11
    daslakndlaajnjndibniaafijdnfijdnsijfnsdinifaafnnasm
    

    Some people might not like my style of coding, but for me, this code is very easy to reason about.

提交回复
热议问题