count consecutive elements

Bischoop Bischoop at vimart.net
Mon Jan 18 22:45:45 EST 2021


On 2021-01-14, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>
>   If you want to know why, maybe you should insert print
>   statements to see the values of critical variables and
>   expression, especially in loops. 
>
>   Then compare the printed values with your expectations.
>
>   Also, decompose into meaningful functions and test each
>   function separately.
>
>

I sat to it again and solved it.

import timeit

run1 = '''

s = 'aabskaaaabadcccc'


lil = tuple(set(s)) # list of characters in s

li=[0,0,0,0,0,0]  # list for counted repeats


for i in lil:
    c = 0
    h= lil.index(i)
    for letter in s:
        if letter == i:
            c += 1
            if c > li[lil.index(letter)]:
                li[lil.index(letter)] = c
        else:
            c=0
            continue

m = max(li)

for index, j in enumerate(li):
    if li[index] == m:
        print(f'{lil[index]} appears {m} consecutive times')


'''
print(timeit.timeit(stmt=run1, number=1))



output:
c appears 4 consecutive times
a appears 4 consecutive times
0.00013008200039621443




More information about the Python-list mailing list