0

Basically say I have a list in python

lst = [6, 6, 7, 1, 1, 1, 1, 4, 1]

the program should return 4 because the number '1' is displayed 4 times in a consecutive manner. Btw the number 6 also displays in a consecutive manner but the program should get the number that shows up the most while being consecutive

more examples:

[6, 4, 4, 4, 1, 1, 7]

should output 3 because '4' is consecutive 3 times

I've been stuck on this problem for a while now, any advice??

CypherX
  • 7,019
  • 3
  • 25
  • 37
Anon
  • 25
  • 6

2 Answers2

2

groupby() will give you individual groups of the same value by default:

> [list(g) for k, g in groupby(lst)] 
[[6, 6], [7], [1, 1, 1, 1], [4], [1]]

You can pass that to max() with a key of len to get the longest one:

> from itertools import group-by
> lst = [6, 6, 7, 1, 1, 1, 1, 4, 1]
> max((list(g) for k, g in groupby(lst)), key=len)
[1, 1, 1, 1]

That's the longest grouping, from which you can get the length. Alternatively you can just build a list of lengths and call max without the key:

max(len(list(g)) for k, g in groupby(lst))
Mark
  • 90,562
  • 7
  • 108
  • 148
0

Two different questions may help.

First to transform the text into list you can check this answer.

Once you have your list, you need to count consecutive values, it was asked and solved here.

Ivan
  • 1,352
  • 2
  • 13
  • 31