Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
max and key function solution in Clear category for The Most Frequent by saklar13
from functools import lru_cache
def count(lst):
@lru_cache() # we don't want to count repeated elements
def counter(elem):
return lst.count(elem)
return counter
def most_frequent(data):
"""
determines the most frequently occurring string in the sequence.
"""
return max(data, key=count(data))
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert most_frequent([
'a', 'b', 'c',
'a', 'b',
'a'
]) == 'a'
assert most_frequent(['a', 'a', 'bi', 'bi', 'bi']) == 'bi'
print('Done')
Oct. 21, 2017