2021-11-07 15:13:58 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
|
|
def mode(input_list: list) -> list[Any]:
|
2019-10-29 23:26:28 +00:00
|
|
|
"""This function returns the mode(Mode as in the measures of
|
|
|
|
central tendency) of the input data.
|
|
|
|
|
|
|
|
The input list may contain any Datastructure or any Datatype.
|
|
|
|
|
2021-11-07 15:13:58 +00:00
|
|
|
>>> mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2])
|
2021-06-04 20:16:32 +00:00
|
|
|
[2]
|
2021-11-07 15:13:58 +00:00
|
|
|
>>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2])
|
2021-06-04 20:16:32 +00:00
|
|
|
[2]
|
2021-11-07 15:13:58 +00:00
|
|
|
>>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2])
|
2021-06-04 20:16:32 +00:00
|
|
|
[2, 4]
|
2021-11-07 15:13:58 +00:00
|
|
|
>>> mode(["x", "y", "y", "z"])
|
2021-06-04 20:16:32 +00:00
|
|
|
['y']
|
2021-11-07 15:13:58 +00:00
|
|
|
>>> mode(["x", "x" , "y", "y", "z"])
|
2021-06-04 20:16:32 +00:00
|
|
|
['x', 'y']
|
2019-10-29 23:26:28 +00:00
|
|
|
"""
|
2021-11-07 15:13:58 +00:00
|
|
|
if not input_list:
|
2021-06-04 20:16:32 +00:00
|
|
|
return []
|
2021-11-07 15:13:58 +00:00
|
|
|
result = [input_list.count(value) for value in input_list]
|
|
|
|
y = max(result) # Gets the maximum count in the input list.
|
2021-06-04 20:16:32 +00:00
|
|
|
# Gets values of modes
|
2021-11-07 15:13:58 +00:00
|
|
|
return sorted({input_list[i] for i, value in enumerate(result) if value == y})
|
2019-10-29 23:26:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-06-04 20:16:32 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|