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])
(input_list: list)
| 2 | |
| 3 | |
| 4 | def mode(input_list: list) -> list[Any]: |
| 5 | """This function returns the mode(Mode as in the measures of |
| 6 | central tendency) of the input data. |
| 7 | |
| 8 | The input list may contain any Datastructure or any Datatype. |
| 9 | |
| 10 | >>> mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]) |
| 11 | [2] |
| 12 | >>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]) |
| 13 | [2] |
| 14 | >>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]) |
| 15 | [2, 4] |
| 16 | >>> mode(["x", "y", "y", "z"]) |
| 17 | ['y'] |
| 18 | >>> mode(["x", "x" , "y", "y", "z"]) |
| 19 | ['x', 'y'] |
| 20 | """ |
| 21 | if not input_list: |
| 22 | return [] |
| 23 | result = [input_list.count(value) for value in input_list] |
| 24 | y = max(result) # Gets the maximum count in the input list. |
| 25 | # Gets values of modes |
| 26 | return sorted({input_list[i] for i, value in enumerate(result) if value == y}) |
| 27 | |
| 28 | |
| 29 | if __name__ == "__main__": |