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>
This commit is contained in:
Grigoriy Hanin 2021-06-04 23:16:32 +03:00 committed by GitHub
parent cb0a5480a7
commit 40e357f688
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,4 @@
import statistics def mode(input_list: list) -> list: # Defining function "mode."
def mode(input_list): # Defining function "mode."
"""This function returns the mode(Mode as in the measures of """This function returns the mode(Mode as in the measures of
central tendency) of the input data. central tendency) of the input data.
@ -9,23 +6,32 @@ def mode(input_list): # Defining function "mode."
>>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]
>>> mode(input_list) >>> mode(input_list)
2 [2]
>>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] >>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]
>>> mode(input_list) == statistics.mode(input_list) >>> mode(input_list)
True [2]
>>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]
>>> mode(input_list)
[2, 4]
>>> input_list = ["x", "y", "y", "z"]
>>> mode(input_list)
['y']
>>> input_list = ["x", "x" , "y", "y", "z"]
>>> mode(input_list)
['x', 'y']
""" """
# Copying input_list to check with the index number later.
check_list = input_list.copy()
result = list() # Empty list to store the counts of elements in input_list result = list() # Empty list to store the counts of elements in input_list
for x in input_list: for x in input_list:
result.append(input_list.count(x)) result.append(input_list.count(x))
input_list.remove(x) if not result:
y = max(result) # Gets the maximum value in the result list. return []
# Returns the value with the maximum number of repetitions. y = max(result) # Gets the maximum value in the result list.
return check_list[result.index(y)] # Gets values of modes
result = {input_list[i] for i, value in enumerate(result) if value == y}
return sorted(result)
if __name__ == "__main__": if __name__ == "__main__":
data = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] import doctest
print(mode(data))
print(statistics.mode(data)) doctest.testmod()