I would have done this that way and using the built in 'max' method for the Counter object
from collections import Counter def max_char(s): return Counter(s).most_common(1)[0][0] if s else ''
To explain a little bit to non Python dev: if s is a valid chain, I build a Counter object with it, get the list of the n (letter, count) elements, getting the first one and returning only the letter.
s
Step by step:
s = 'abcccccccd' Counter(s).most_common(1) # [('c', 7)] Counter(s).most_common(1)[0] # ('c', 7) Counter(s).most_common(1)[0][0] # 'c
Wow, this looked enigmatic at first, but I understood how it works. Thank you!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I would have done this that way and using the built in 'max' method for the Counter object
To explain a little bit to non Python dev:
if
s
is a valid chain, I build a Counter object with it, get the list of the n (letter, count) elements, getting the first one and returning only the letter.Step by step:
Wow, this looked enigmatic at first, but I understood how it works. Thank you!