-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode.py
More file actions
53 lines (38 loc) · 1.22 KB
/
mode.py
File metadata and controls
53 lines (38 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Finding the mode in a list
Author: Oğuzhan Çölkesen
"""
def compute_mode(scores):
""" Returns the mode(s) of the supplied data.
In case the mode is not unique, then the list of modes returned is in
ascending order.
Parameters:
scores - a list of integers denoting the data whose mode we wish to
compute.
Returns:
A list of integers denoting the mode(s) of the data (sorted in
ascending order).
"""
scores_dictionary = {}
for i in scores:
if i in scores_dictionary:
scores_dictionary[i] += 1
else:
scores_dictionary[i] = 1
maximum_occurance = 0
for i in scores_dictionary:
maximum_occurance = max(maximum_occurance, scores_dictionary[i])
modes = []
for i in scores:
if (scores_dictionary[i] == maximum_occurance and i not in modes):
modes.append(i)
modes.sort()
return modes
def main():
""" Tester function """
print("Testing compute_mode")
print(compute_mode([65, 70, 88, 70]))
print(compute_mode([88, 70, 65, 70, 88]))
print(compute_mode([92, 56, 14, 73, 22, 38, 93, 45, 55]))
if __name__ == "__main__":
main()