user15271588
user15271588

Reputation:

how to count a repeated char from a string

imagine you have this string

'49999494949'
'4949999949'
'4949499994'

how do you count the most repeated of this 9 in one line after the 4 and print it out as '99999'

i tried

a = '49999494949'
b = "123456780"
for char in b:
    a = a.replace(char, "")

return a

but it's end up giving me all the 9 in the given string where i want only the most repeated 9 in line after the 4

and thanks for helping !

Upvotes: 0

Views: 42

Answers (1)

ddejohn
ddejohn

Reputation: 8962

You can split each string on "4", which will give you a list of strings containing "9". Then you can find the longest:

>>> s = "49999494949"
>>> nines = s.split("4")
>>> nines
['', '9999', '9', '9', '9']
>>> max(nines)
'9999'

Or as a single command:

>>> s = "49999494949"
>>> max(s.split("4"))
'9999'

Upvotes: 1

Related Questions