-
Notifications
You must be signed in to change notification settings - Fork 6
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
Most common element #29
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, if we were more series, I would expect improving with Counter
, and likely have another iteration (for some other nitpicky/small thing).
As is, I think this is a great PR start. (A PR without any iteration is very rare even for year-long maintainers. Up to: "I am worried if something goes in without a comment").
So LGTM thanks you. I'll just apply the comment removal first.
counting[i] += 1 | ||
else: | ||
counting[i] = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also use from collections import Counter
to avoid the if
clause even. But considering how basic the project, is I think this is a perfect solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the update. I was not aware of Counter
.
def most_common_element(ll):
if not isinstance(ll, list):
raise ValueError("Input must be a list")
c = Counter(ll)
return c.most_common(1)[0][0]
What do you mean by "likely have another iteration (for some other nitpicky/small thing)", please? Happy to learn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant that it is not uncommon for someone to have small (often tiny/silly) asks to improve things a bit more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to improve, thank you for the input :-)
Ah, we could have added a |
We could have added them but do we want to close the issues? Just in case we use the material again in the future. |
Ah, good point. I closed the issues now... Maybe it makes sense to just reopen them anyway also (since they are linked, etc.)? |
Sounds good to me! |
Most common element - update for #29
This is my suggestion for an implementation of the function
most_common_element
with pure Python commands, not using numpy. :-)A test was provided.