This exercise is about automatic vote counting, every voter chooses his or her favorite candidate and writes it down on a ballot. The \(k\) candidates with the most votes are elected.
Design and implement an algorithm for this problem. Determine the timecomplexity of your algorithm.
Write a Python function count_votes(amount: int, votes: list)
. This function takes the number of elected persons and the list of forms as it’s arguments and returns the set of elected persons. When there is an ex aequo with determining the \(k\)-th elected person, choose a random person.
>>> count_votes(2, ["Piet", "Joris", "Piet", "Corneel", "Piet", "Jan", "Corneel", "Jan", "Corneel", "Corneel", "Corneel"])
{'Corneel', 'Piet'}
>>> count_votes(2, ["Joris","Herman","Nathan","Herman","Joris","Nathan","Nathan","Felix","Felix","Herman","Piet","Herman","Herman","Joris","Felix","Herman","Corneel","Joris"])
{'Herman', 'Joris'}
>>> count_votes(4, ["Herman","Corneel","Nathan","Felix","Corneel","Margot","Piet","Herman","Felix","Jan","Felix","Herman","Nathan","Herman","Jan","Jan","Piet","Jan","Margot","Herman","Felix","Joris","Jan","Jan","Nathan"])
{'Felix', 'Herman', 'Jan', 'Nathan'}
>>> count_votes(3, ["Nathan","Margot","Margot","Karen","Felix","Felix","Karen","Herman","Karen","Herman","Karen","Herman","Chloë","Herman","Nathan","Margot","Margot","Herman","Nathan","Felix","Herman","Karen","Karen"])
{'Herman', 'Karen', 'Margot'}