Simple Voting Scheme
Write a Python program that reads a file containing votes
indicated by name of candidate, one per line, and counts and
declares the winner (candidate with most number of votes).
If there is a tie for first place, all candidates are declared
the winner. Here are the contents of a possible input file and a sample run of the
program:
mirage:02-vote raj$ more vote1.dat
Red
Blue
Green
Blue
Blue
Red
mirage:02-vote raj$ python3 Vote1.py vote1.dat
Votes: ['Red', 'Blue', 'Green', 'Blue', 'Blue', 'Red']
Winner: Blue
Here is an example with a tie for first place:
mirage:02-vote raj$ more vote1.dat
Red
Blue
Green
Blue
Blue
Red
Red
mirage:02-vote raj$ python3 Vote1.py vote1.dat
Votes: ['Red', 'Blue', 'Green', 'Blue', 'Blue', 'Red', 'Red']
Winner: Blue
Winner: Red
Ranked Voting Scheme
In this modified voting scheme, each ballot lists 1st preference, 2nd preference, etc. The
voting ballots are again present in a text file with each line corresponding to a
single ballot. The candidates are listed in preference order and are separated by
commas. Here is a sample input file:
mirage:02-vote raj$ more vote2.dat
Red,Green
Blue
Green,Red,Blue
Blue,Green,Red
Green
The method to determine the winner proceeds by eliminating candidate(s) with lowest number of
1st choice votes (if there are ties, eliminate all). This process is repeated until a single candidate
remains. That candidate is declared the winner. If by eliminating candidates in any one round no candidate
remains then all the candidates remaining in that round are declared the winner.
Mac-mini:02-vote raj$ python3 Vote2.py vote2.dat
Ballots: [['Red', 'Green'], ['Blue'], ['Green', 'Red', 'Blue'], ['Blue', 'Green', 'Red'], ['Green']]
Winner: Green