Deciding the Election The following algorithm is used to decide who has won the
ID: 3844503 • Letter: D
Question
Deciding the Election The following algorithm is used to decide who has won the election. The number of papers remaining after informal votes have been removed is the pool of "elegible vote 1. In the first step, the first preferences from all the votes are counted. If a candidate secures at least 50% of the eligible votes, that candidate is declared elected and no further counting is done 2. If no candidate has secured an absolute majority (i.e. minimum 50% of the elegible votes, plus 1, the candidate with smallest tally is eliminated from further counting, and that candidate's preferences are added to the counts of the remaining candidates. If a candidate now has an absolute majority the votes, that candidate is declared elected. Otherwise, Step 2 is repeated: The candidate with smallest number of votes is eliminated and the next-preference votes redistributed among the remaining candidates. That is, for each vote associated with the eliminated candidate the current top preference (for the eliminated candidate) is removed and the next preference is instead associated with the next most favoured candidate. However, if the next most favoured candidate has also been eliminated, the preference is popped and the preference after that should be used, and so on. If all the preferences for a papers have been used, the vote is regarded as "spent and discarded 4. In the event of a tie for smallest count, the Act provides for the Returning Officer, i e. person in charge of the count o, first, recount and then, if the tie remains, to make a random choice among the candidates who have equal smallest counts. For this Project, however, please choose for elimination the candidate nearest the top of the list of candidates. (This provides reproducible behaviour when testing Optional Preferential Optional Preferential Voting is to also be modelled in this Project. Optional Preferential Voting is simply the voting method that allows incomplete ballots to be accepted, so long as at least one candidate receives a vote. In other words, if there are 5 candidates in the election for a seat, an elector can number, say, 3 squares, so long as no two squares have the same number and no numbers from 1 to 3 are missing Your Program Your program must contain a definition for the function main, starting with def main candidates file ballots file name optional False this case, the As in Projectl, main will be used to call your code durin g automated testing. However, call to main will also be used to provide the file-name for the file of candidates names and the file-name of the file of ballots. Your program should therefore NOT call input The file containing names of candidates is exactly as per Projectl: one name per line The votes, i.e. papers, will be in a comma separated file, e.g 1, 3, 4, 5, 2 indicating that Candidate 1 has the first preference, Candidate 5 has the second preference Candidate 2 has the third preference, etc. Other lines will look like: 2, 1, 3, 5, 4 1, 3, 4, 5, 2 3, 5,2, 4,1 1, 4, 5, 2, 3 If optional preferential voting is allowed, these votes are also formal:Explanation / Answer
I have desgined and developed the Python Program for implementing the algorithm for the election scenario. I have included the comments for each part of code and attached the final output of it.
Let me explain you in brief and in step-by-step manner:-
Step-1:
The initial part is to create a method which basically calculates the first preferences to check whether he got the count which accounts half of the election votes, then he is declared as a Winner and no counting will occur again.
Example:-
def __init__(self, electionFile):
self.electionCandidates = set()
self.electionScores = dict()
self.electionResults = dict()
self.electionWinner = None
self.electionVotersList = list()
self.electionFile = electionFile
Step-2:
The next step is to find the second largest voters majority and start counting for the respective voting systems, Remember to remove the least votes count people from the election list.
Example:-
def electionProcess(self):
self.electionData()
self.electionDict()
self.electionTieCandidates()
self.electionWinnerName()
return self.electionWinner
Step-3:
The final step is to check the condition of a tie where people with same number of votes, The result is to be declared in the form of recounting it again and eliminates the least votes in specific regions.
Python Program:-
# It is the import method used to embedd methods and classes
import sys
class electionScenario:
# The def __init__ is the starting point of a program
def __init__(self, electionFile):
self.electionCandidates = set()
self.electionScores = dict()
self.electionResults = dict()
self.electionWinner = None
self.electionVotersList = list()
self.electionFile = electionFile
def electionProcess(self):
self.electionData()
self.electionDict()
self.electionTieCandidates()
self.electionWinnerName()
return self.electionWinner
def electionData(self):
with open(self.electionFile, encoding='utf-8') as electionFiles:
for electionLines in electionFiles:
(voterOne, voterTwo, voterThree, voterFour) = electionLines.split(None, 4)
self.electionVotersList.append((voterOne, voterTwo, voterThree, voterFour))
def electionDict(self):
for electionVotingSystem in self.electionVotersList:
for electionCandidates in electionVotingSystem:
self.electionCandidates.add(electionCandidates)
for electionPair in list(itertools.permutations(electionVotingSystem, 2)):
if electionPair not in self.electionScores:
self.electionScores[electionPair] = 0
if electionVotingSystem.index(electionPair[0]) < electionVotingSystem.index(electionPair[1]):
self.electionScores[electionPair] += 1
def electionTieCandidates(self):
for electionTie in list(itertools.combinations(self.electionCandidates, 2)):
reverse = tuple(reversed(electionTie))
if self.electionScores[electionTie] > self.electionScores[reverse]:
self.electionResults[electionTie] = electionTie[0]
else:
self.electionResults[electionTie] = electionTie[1]
def electionWinnerName(self):
for electionCandidates in self.electionCandidates:
candidate_score = 0
for result in self.electionResults:
if electionCandidates in result and self.electionResults[result] == electionCandidates:
candidate_score += 1
if candidate_score == len(self.electionCandidates) - 1:
self.electionWinner = electionCandidates
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.