-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Mistake in maths/average_mode.py fixed. #4464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4af79a8
* Mistake in average_mode.py fixed. The previous solution was to return
haningrisha dcdcc9e
redundant check_list deleted
haningrisha fe6ca24
Suggestions resolved
haningrisha 117524f
Merge branch 'mode_fix' of https://github.com/haningrisha/Python into…
haningrisha 8ab8134
Black done
haningrisha 6ae7563
Unused statistics import
haningrisha ba7af02
Several modes support added
haningrisha 7d4fca8
Comment fix
haningrisha 0ef1273
Update maths/average_mode.py
haningrisha 60d97fb
Suggestions added
haningrisha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,31 +1,36 @@ | ||||||||
import statistics | ||||||||
|
||||||||
|
||||||||
def mode(input_list): # Defining function "mode." | ||||||||
def mode(input_list: list) -> list: # Defining function "mode." | ||||||||
"""This function returns the mode(Mode as in the measures of | ||||||||
central tendency) of the input data. | ||||||||
|
||||||||
The input list may contain any Datastructure or any Datatype. | ||||||||
|
||||||||
>>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] | ||||||||
>>> mode(input_list) | ||||||||
2 | ||||||||
>>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] | ||||||||
>>> mode(input_list) == statistics.mode(input_list) | ||||||||
True | ||||||||
[2] | ||||||||
>>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2] | ||||||||
>>> mode(input_list) | ||||||||
[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 | ||||||||
for x in input_list: | ||||||||
result.append(input_list.count(x)) | ||||||||
input_list.remove(x) | ||||||||
y = max(result) # Gets the maximum value in the result list. | ||||||||
# Returns the value with the maximum number of repetitions. | ||||||||
return check_list[result.index(y)] | ||||||||
y = max(result) # Gets the maximum value in the result list. | ||||||||
haningrisha marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
# Gets values of modes | ||||||||
result = [input_list[i] for i, value in enumerate(result) if value == y] | ||||||||
result = list(set(result)) # Deletes duplicates | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of creating a list, then converting it to a set, and then back to a list, you could do a set comprehension. Also,
Suggested change
|
||||||||
return sorted(result) | ||||||||
|
||||||||
|
||||||||
if __name__ == "__main__": | ||||||||
data = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] | ||||||||
print(mode(data)) | ||||||||
print(statistics.mode(data)) | ||||||||
import doctest | ||||||||
|
||||||||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.