|
| 1 | +import statistics |
| 2 | + |
| 3 | + |
| 4 | +def mode(input_list): # Defining function "mode." |
| 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 | + >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] |
| 11 | + >>> mode(input_list) |
| 12 | + 2 |
| 13 | + >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] |
| 14 | + >>> mode(input_list) == statistics.mode(input_list) |
| 15 | + True |
| 16 | + """ |
| 17 | + # Copying inputlist to check with the index number later. |
| 18 | + check_list = input_list.copy() |
| 19 | + result = list() # Empty list to store the counts of elements in input_list |
| 20 | + for x in input_list: |
| 21 | + result.append(input_list.count(x)) |
| 22 | + input_list.remove(x) |
| 23 | + y = max(result) # Gets the maximum value in the result list. |
| 24 | + # Returns the value with the maximum number of repetitions. |
| 25 | + return check_list[result.index(y)] |
| 26 | + |
| 27 | + |
| 28 | +if __name__ == "__main__": |
| 29 | + data = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] |
| 30 | + print(mode(data)) |
| 31 | + print(statistics.mode(data)) |
0 commit comments