mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
a98465230f
* [mypy] Fix annotations in `maths/series/p_series.py` * Update p_series.py * Update p_series.py * Remove from excluded in mypy.ini * Type annotation for series * Annotate maths/proth_number.py (properly) * Remove from excluded in mypy.ini * Annotate average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Remove from excluded in mypy.ini * Fix annotations in gamma_recursive.py * Remove from excluded in mypy.ini * Annotations for geometric_series.py * Update geometric_series.py * Update mypy.ini * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update mypy.ini * Update mypy.ini * Update mypy.ini * Update average_mode.py * Update proth_number.py * Update average_mode.py * Update gamma_recursive.py * Update proth_number.py * Update mypy.ini * Update geometric_series.py * Update average_mode.py * Update proth_number.py * Update geometric_series.py * Update geometric_series.py * Update geometric_series.py * Update p_series.py * Update geometric_series.py * Update p_series.py * Update p_series.py * Update geometric_series.py * Update p_series.py * Update p_series.py * Remove data_structures/stacks/next_greater_element.py| Co-authored-by: Christian Clauss <cclauss@me.com>
33 lines
935 B
Python
33 lines
935 B
Python
from typing import Any
|
|
|
|
|
|
def mode(input_list: list) -> list[Any]:
|
|
"""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.
|
|
|
|
>>> mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2])
|
|
[2]
|
|
>>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2])
|
|
[2]
|
|
>>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2])
|
|
[2, 4]
|
|
>>> mode(["x", "y", "y", "z"])
|
|
['y']
|
|
>>> mode(["x", "x" , "y", "y", "z"])
|
|
['x', 'y']
|
|
"""
|
|
if not input_list:
|
|
return []
|
|
result = [input_list.count(value) for value in input_list]
|
|
y = max(result) # Gets the maximum count in the input list.
|
|
# Gets values of modes
|
|
return sorted({input_list[i] for i, value in enumerate(result) if value == y})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|