Added Majority Voting Algorithm #9866
Conversation
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
for more information, see https://pre-commit.ci
Co-authored-by: Christian Clauss <cclauss@me.com>
for more information, see https://pre-commit.ci
other/majority_vote_algorithm.py
Outdated
| """ | ||
|
|
||
|
|
||
| def majority_element(total_votes: list[int], min_votes_required: int) -> list[int]: |
There was a problem hiding this comment.
If partition is min_votes_required then how does this algorithm use a constant number of words of memory as discussed in the Wikipedia article? If there are 30 candidates then there will ~10 times as much memory used than 3 candidates.
There was a problem hiding this comment.
Problem : We need to find the elements (votes or candidates) that appear at least floor( total votes / k) times ,( here k is partitioning the array i.e. total_votes, that's why I named it partition before ). We need to solve in a linear time. This is not representing real world voting.
This was a problem came in an interview that I attended. Assuming that we have all the votes and the factor is fixed like 2 or 3. What I have contributed is a general case of the algorithm.
There was a problem hiding this comment.
I know this is not very special algo but It was something new in the interview and I wanted to contribute for the Hacktoberfest and that's the reason I submitted this.
Co-authored-by: Christian Clauss <cclauss@me.com>
for more information, see https://pre-commit.ci
Co-authored-by: Christian Clauss <cclauss@me.com>
for more information, see https://pre-commit.ci
|
OK... Following the pseudocode and the graph in the wiki article... We need two functions. The candidates are: |
cclauss
left a comment
There was a problem hiding this comment.
Nice! Thanks for your patience!
|
Thank you so much, this was my first time, finally did it. Thanks a lot for the support and sorry for the mistakes. Keep up the good work. God bless you. |
Describe your change:
Checklist: