Skip to content

Commit 6343361

Browse files
LokiUvarajcclauss
authored andcommitted
average_mode.py (TheAlgorithms#1491)
* Add files via upload Finds the mode in the input data. * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Tabs do not belong in Python files!
1 parent 53ff735 commit 6343361

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

maths/average_mode.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

Comments
 (0)