Skip to content

Commit 40e357f

Browse files
Mistake in maths/average_mode.py fixed. (#4464)
A serious bug was addressed with this pull request. The mode function previously didn't return the mode of the input list. Instead, it always returned the first value. Due to lacking tests, the bug was never caught. This pull request also adds new functionality to the function, allowing support for more than one mode. See #4464 for details. * * Mistake in average_mode.py fixed. The previous solution was to returnthe value on the first loop iteration, which is not correct, more than that it used to delete repeating values, so result's array and check array lost relation between each other * Type hint added * redundant check_list deleted Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> * Suggestions resolved * output typing changed to Any * test cases added * Black done File formatted * Unused statistics import statistics only used in doctest, now they are imported in doctest * Several modes support added Several modes support added * Comment fix * Update maths/average_mode.py Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> * Suggestions added Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com>
1 parent cb0a548 commit 40e357f

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

maths/average_mode.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1-
import statistics
2-
3-
4-
def mode(input_list): # Defining function "mode."
1+
def mode(input_list: list) -> list: # Defining function "mode."
52
"""This function returns the mode(Mode as in the measures of
63
central tendency) of the input data.
74
85
The input list may contain any Datastructure or any Datatype.
96
107
>>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]
118
>>> 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
9+
[2]
10+
>>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]
11+
>>> mode(input_list)
12+
[2]
13+
>>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]
14+
>>> mode(input_list)
15+
[2, 4]
16+
>>> input_list = ["x", "y", "y", "z"]
17+
>>> mode(input_list)
18+
['y']
19+
>>> input_list = ["x", "x" , "y", "y", "z"]
20+
>>> mode(input_list)
21+
['x', 'y']
1622
"""
17-
# Copying input_list to check with the index number later.
18-
check_list = input_list.copy()
1923
result = list() # Empty list to store the counts of elements in input_list
2024
for x in input_list:
2125
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+
if not result:
27+
return []
28+
y = max(result) # Gets the maximum value in the result list.
29+
# Gets values of modes
30+
result = {input_list[i] for i, value in enumerate(result) if value == y}
31+
return sorted(result)
2632

2733

2834
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))
35+
import doctest
36+
37+
doctest.testmod()

0 commit comments

Comments
 (0)