def runTestWithRandomTiebreakBetweenTwoLosers(): description = "Random Tiebreak Between Losing Candidates Test" print(description) print("Generating ballots...") candidateOne = "Candidate One" candidateTwo = "Candidate Two" candidateThree = "Candidate Three" candidateFour = "Candidate Four" num_ballots = 15 ballots = set() # Candidate-One-preferred ballots for i in xrange(6): ballot = Ballot() ballot.set_candidate_with_rank(candidateOne, 1) ballots.add(ballot) # Candidate-Two-preferred ballots for i in xrange(5): ballot = Ballot() ballot.set_candidate_with_rank(candidateTwo, 1) ballots.add(ballot) # Candidate-Three-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(candidateThree, 1) ballot.set_candidate_with_rank(candidateFour, 2) ballots.add(ballot) # Candidate-Four-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(candidateFour, 1) ballot.set_candidate_with_rank(candidateThree, 2) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 2 election.ballots = ballots election.is_final_tiebreak_manual = True winners = election.compute_winners(verbose=True) print(winners)
def main(): no_confidence = Ballot.NO_CONFIDENCE no_confidence_short = "NC" run_command = "run" file_command = "file" quit_command = "quit" undo_command = "undo" help_command = "help" election_name = None election_seats = None election_ballots = list() print_introduction() election_name = prompt_election_name() election_seats = prompt_election_seats() print_ballot_instructions(no_confidence_short, run_command, quit_command, undo_command, file_command, help_command) while True: ballot_or_command = prompt_ballot_command(len(election_ballots)) # Empty input if len(ballot_or_command) == 0: continue # 'run' command elif ballot_or_command.lower() == run_command: # Create the Election election = Election(name=election_name, seats=election_seats) election.is_final_tiebreak_manual = True election.ballots = set(election_ballots) # Run the election and compute winners winners = election.compute_winners(verbose=True)[0] # Print the winners and quit the program print_conclusion(winners, election_seats) return 0 # 'quit' command elif ballot_or_command.lower() == quit_command: # Quit the program return 0 # 'undo' command elif ballot_or_command.lower() == undo_command: # Pop the last ballot from the list of ballots if len(election_ballots) > 0: election_ballots.pop() print("The most-recent ballot has been removed.") else: print("There are no ballots to remove.") # 'help' command elif ballot_or_command.lower() == help_command: # Print the instructions again print_ballot_instructions(no_confidence_short, run_command, quit_command, undo_command, file_command, help_command) # 'file' command elif ballot_or_command.lower().startswith(file_command): filename = ballot_or_command[len('find '):] with open(filename) as file: # For each ballot in the CSV for ranked_candidates in csv.reader(file): ballot = Ballot() for rank, candidate in enumerate(ranked_candidates): if candidate: ballot.set_candidate_with_rank(candidate, rank + 1) # Add the ballot to the list of ballots election_ballots.append(ballot) # Ballot input else: # Parse the input into a list of strings representing candidates ranked_candidates = [candidate_input.strip() for candidate_input in ballot_or_command.split(',')] # For each candidate in the list, add them to the Ballot with increasing rank ballot = Ballot() rank = 1 for candidate in ranked_candidates: # Replace No Confidence abbreviations if candidate == no_confidence_short: candidate = no_confidence ballot.set_candidate_with_rank(candidate, rank) rank += 1 # Add the ballot to the list of ballots election_ballots.append(ballot)