0

When I test my code I get the correct output but check50 shows errors in print_winner function.

:( print_winner prints winner of election when one candidate wins over all others print_winner did not print winner of election

:( print_winner prints winner of election when some pairs are tied print_winner did not print winner of election

Any help is appreciated,

My Code: https://pastebin.com/G8avSiCg

EDIT: Fixed it but don't understand what was wrong with the old code.

Solved it here: https://pastebin.com/GEpuXWEQ

1

Your old code was going through the first n pairs, where n is the number of candidates, then checking if one of the winners of those pairs is the total winner. You can't assume that with n candidates, the overall winner will be the winner of one of the first n pairs.

Example: If there are 9 voters and 6 candidates

5 people vote A, B, C, D, E, F
4 people vote B, C, D, E, F, A

Your sorted pairs array will start with

B -> C
B -> D
B -> E
B -> F
C -> D
C -> E
C -> F
D -> E
D -> F
E -> F

all with 9 people voting the first candidate over the second

then the pairs will have

A -> B
A -> C
A -> D
A -> E
A -> F

all with 5 people preferring A and 4 people preferring the other candidate.

Candidate A is the winner here, but your first algorithm will never get to a pair where they win, because it'll stop after the 6th pair.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .