Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write program in python coding language for the following scenario. Input data is a csv file shown below which details sample voting ballots: Data
Please write program in python coding language for the following scenario.
Input data is a csv file shown below which details sample voting ballots:
Data is imported using the following code:
Introduction: Rank-Ordered Voting In rank-ordered elections, each voter orders the candidates from most preferred (1st choice) to least preferred (Nth choice) instead of only voting for one person. All of the first choice votes are counted, if one candidate receives 50% + 1 votes, then election is decided. Other- wise: 1. Eliminate the single remaining candidate with the fewest votes. (a) If there is a tie for the candidates with the lowest votes, flip a coin to decide whom to eliminate. (b) Add the newly eliminated candidate to the list of previously candidates elimi 2. Move all votes for the newly eliminated candidate to the next highest preference of each ballot. Skip any candidates that have already been eliminated 3. Set aside any ballots that contain no remaining candidates. 4. If two candidates remain, then stop (a) If one candidate has higher number of votes, they are the winner. (b) Else, if there is a tie between the two remaining candidates, declare the election as Tied". 5. Else, if one candidate now has 50% + 1 votes, then stop; the election is decided. Other- wise, goto step 1. Each pass through Steps 1 - 5 is called an election round. Each round only looks at the 1st choice votes. Choice ;3 Choice .2 Xian Xian Elvis Bridgette Xian Xian Xian Elvis Bridgette 2 Choice1 Alejandro Elvis Bridgette Xian Kanishka Alejandro Elvis Bridgette Elvis Alejandro Alejandro Elvis Elvis Elvis Alejandro Alejandro Elvis Elvis Alejandro Xian Alejandro Elvis Xian Xian Elvis Alejandro ridgette ian Alejandro Elvis Bridgette Xian Elvis Bridgette Xian Xian Elvis Bridgette Elvis Alejandro Alejandro Elvis 1 ### lecture 5 2 3 ## Authenticate 4 from google.colab import drive 5 drive.mount( '/content/gdrive') 7 List files in this directory 8 print (nFiles in the directory:" ) 9 !1s /content/gdrive/My\ Drive/CBE-20258-Colab 10 11 ## specify path to your folder 12 # Change this if you want to access files in a different Google Drive folder. 13 my_drive - "/content/gdrive/My Drive/CBE-20258-Colab/" 1 ### tutorial 2 2 import csv 4 # function to read and store data from csv files 5 def read_data(tutorial _sample vote): this function is used to read-in csv files containing voting ballots and return a list of voting ballots 9 10 arguments: tutorial_sample_vote: csv file containing ballots from voting file 12 13 14 15 16 17 18 19 20 21 returns: vote_data: list of lists containing vote-ballot data vote_position: list of indices which shows the index of 1st-choice oni each ballot # reading in csv file with ballot information # create empty lists to store vote data and vote position vote data - [1 vote position - [1 23 2 4 25 26 27 28 29 30 31 32 # open the text file containing ballot data with open (tutorial_sample_vote) as vote_file: ballot file csv.reader(vote file) # loop through each row in the ballot file for i,row in enumerate (ballot file): # skip the first row- it is the header 3 4 35 # add the data in the row to the list 'vote data vote data.append (row) 3 6 37 38 39 40 41 42 43 set the vote position to zero vote position.append (0) print (vote data,vote position) # return both the vote data and vote position lists return vote data, vote position 1 tutorial 2 3 # mount Google Drive to read data files 4 from google.colab import drive 5 #drive.mount(./content/gdrive.) 7 my drive /content/gdrive/My Drive/CBE-20258-Colab/ 9 # use function 10 votedata,vote position -read data (my drive+ 'tutorial sample_vote.csv) 'Alejandro'Xian', 'Elvis 11 [01 'Alejandro', 'Xian',Elvis'1, ['Elvis', 'Xian','Alejandro'1] [0, 0 'Alejandro, 'xian', 'Elvis', 'Elvis' 'Xian', 'Alejandro', ['Bridgette, 'Elvis,"Alejandro'1 [0, 0, 01 'Alejandro, 'Xian', 'Elvis', 'Elvis',"Xian', Alejandro', 'Bridgette', 'Elvis', "Alejandro', ['Xian', 'Bridgette, Elvis'1 [0, 0, 0, 0] 'Alejandro', 'Xian', Elvis'1, ['Elvis', 'Xian', 'Alejandro', ['Bridgette''Elvis', 'Alejandro'], 'Xian', 'Bridgette', 'Elvis'], 'Kanishka', "xian', 'Alejandro', 'Xian',Elvis'1, 'Elvis', 'Xian','Alejandro'1, 'Bridgette'Elvis',Alejandro', 'xian','Bridgette' 'Elvis','Kanishka", "'Xian' [ [ "Alejandro' , 'Xian' , 'Elvis ' ] , [ 'Elvis' , "Xian' , . Alejandro' ], [ ' Bridgette' , 'Elvis' , .ALejandro' ] t'x1an' , 'Bridgette' , 'Elvis' ] , Elvis', 'Kanishka', "Xian, 'Elvis'], [ Kanishka' , .x1an. 'Alejandro,'Xian, 'Elvis', ['Elvis', 'Xian', Alejandro', 'Bridgette', 'Elvis', "Alejandro'], ['Xian', 'Bridgette [[ 'Alejandro', 'Xian', 'Elvis'), ['Elvis', .Xian. .Alejandro'], ['Bridgette', 'Elvis'' .ALejandro'], ["Xian', 'Bridgette', ['Kanishka'' Xian. 'Alejandro, 'Xian', 'Elvis', 'Elvis', 'Xian', Alejandro', 'Bridgette', 'Elvis, "Alejandro', ['xian', 'Bridgette', Elvis', 'Kanishka', "Xian' 'Alejandro, 'Xian, 'Elvis, 'Elvis',"Xian', Alejandro', 'Bridgette', 'Elvis, "Alejandro', ['Xian', 'Bridgette, Elvis', 'Kanishka', "Xian' 'Alejandro', 'Xian', Elvis' 1, ['Elvis', 'Xian', 'Alejandro, ['Bridgette','Elvis', 'Alejandro', 'Xian', 'Bridgette, 'Elvis'], 'Kanishka', "xian', [['Alejandro', .Xian', 'Elvis'), ['Elvis'' Xian. .Alejandro'], ['Bridgette', 'Elvis'' 'Alejandro'), ["Xian', 'Bridgette', 'Elvis'], ['Kanishka', 'Xian. 'Alejandro, 'Xian', 'Elvis', ['Elvis', Xian', 'Alejandro, 'Bridgette, 'Elvis, "Alejandro, ['Xian*,'Bridgette, 'Elvis , ['Kanishka', "Xian', 'Alejandro,'Xian, 'Elvis, 'Elvis',"Xian', Alejandro', 'Bridgette', 'Elvis', "Alejandro', ['Xian', 'Bridgette Elvis', Kanishka', "Xian', 'Alejandro', 'Xian', 'Elvis', ['Elvis', 'Xian', 'Alejandro' l, ['Bridgette'Elvis', Alejandro' ], ['xian', "Bridgette', 'Elvis', 'Kanishka', "'Xian', I Alejandro', 'Xian', 'Elvis', 'Elvis',xian,Alejandro, 'Bridgette',Elvis',Alejandro'], ['Xian', 'Bridgette', 'Elvis'1, ['Kanishka', 'xian', Introduction: Rank-Ordered Voting In rank-ordered elections, each voter orders the candidates from most preferred (1st choice) to least preferred (Nth choice) instead of only voting for one person. All of the first choice votes are counted, if one candidate receives 50% + 1 votes, then election is decided. Other- wise: 1. Eliminate the single remaining candidate with the fewest votes. (a) If there is a tie for the candidates with the lowest votes, flip a coin to decide whom to eliminate. (b) Add the newly eliminated candidate to the list of previously candidates elimi 2. Move all votes for the newly eliminated candidate to the next highest preference of each ballot. Skip any candidates that have already been eliminated 3. Set aside any ballots that contain no remaining candidates. 4. If two candidates remain, then stop (a) If one candidate has higher number of votes, they are the winner. (b) Else, if there is a tie between the two remaining candidates, declare the election as Tied". 5. Else, if one candidate now has 50% + 1 votes, then stop; the election is decided. Other- wise, goto step 1. Each pass through Steps 1 - 5 is called an election round. Each round only looks at the 1st choice votes. Choice ;3 Choice .2 Xian Xian Elvis Bridgette Xian Xian Xian Elvis Bridgette 2 Choice1 Alejandro Elvis Bridgette Xian Kanishka Alejandro Elvis Bridgette Elvis Alejandro Alejandro Elvis Elvis Elvis Alejandro Alejandro Elvis Elvis Alejandro Xian Alejandro Elvis Xian Xian Elvis Alejandro ridgette ian Alejandro Elvis Bridgette Xian Elvis Bridgette Xian Xian Elvis Bridgette Elvis Alejandro Alejandro Elvis 1 ### lecture 5 2 3 ## Authenticate 4 from google.colab import drive 5 drive.mount( '/content/gdrive') 7 List files in this directory 8 print (nFiles in the directory:" ) 9 !1s /content/gdrive/My\ Drive/CBE-20258-Colab 10 11 ## specify path to your folder 12 # Change this if you want to access files in a different Google Drive folder. 13 my_drive - "/content/gdrive/My Drive/CBE-20258-Colab/" 1 ### tutorial 2 2 import csv 4 # function to read and store data from csv files 5 def read_data(tutorial _sample vote): this function is used to read-in csv files containing voting ballots and return a list of voting ballots 9 10 arguments: tutorial_sample_vote: csv file containing ballots from voting file 12 13 14 15 16 17 18 19 20 21 returns: vote_data: list of lists containing vote-ballot data vote_position: list of indices which shows the index of 1st-choice oni each ballot # reading in csv file with ballot information # create empty lists to store vote data and vote position vote data - [1 vote position - [1 23 2 4 25 26 27 28 29 30 31 32 # open the text file containing ballot data with open (tutorial_sample_vote) as vote_file: ballot file csv.reader(vote file) # loop through each row in the ballot file for i,row in enumerate (ballot file): # skip the first row- it is the header 3 4 35 # add the data in the row to the list 'vote data vote data.append (row) 3 6 37 38 39 40 41 42 43 set the vote position to zero vote position.append (0) print (vote data,vote position) # return both the vote data and vote position lists return vote data, vote position 1 tutorial 2 3 # mount Google Drive to read data files 4 from google.colab import drive 5 #drive.mount(./content/gdrive.) 7 my drive /content/gdrive/My Drive/CBE-20258-Colab/ 9 # use function 10 votedata,vote position -read data (my drive+ 'tutorial sample_vote.csv) 'Alejandro'Xian', 'Elvis 11 [01 'Alejandro', 'Xian',Elvis'1, ['Elvis', 'Xian','Alejandro'1] [0, 0 'Alejandro, 'xian', 'Elvis', 'Elvis' 'Xian', 'Alejandro', ['Bridgette, 'Elvis,"Alejandro'1 [0, 0, 01 'Alejandro, 'Xian', 'Elvis', 'Elvis',"Xian', Alejandro', 'Bridgette', 'Elvis', "Alejandro', ['Xian', 'Bridgette, Elvis'1 [0, 0, 0, 0] 'Alejandro', 'Xian', Elvis'1, ['Elvis', 'Xian', 'Alejandro', ['Bridgette''Elvis', 'Alejandro'], 'Xian', 'Bridgette', 'Elvis'], 'Kanishka', "xian', 'Alejandro', 'Xian',Elvis'1, 'Elvis', 'Xian','Alejandro'1, 'Bridgette'Elvis',Alejandro', 'xian','Bridgette' 'Elvis','Kanishka", "'Xian' [ [ "Alejandro' , 'Xian' , 'Elvis ' ] , [ 'Elvis' , "Xian' , . Alejandro' ], [ ' Bridgette' , 'Elvis' , .ALejandro' ] t'x1an' , 'Bridgette' , 'Elvis' ] , Elvis', 'Kanishka', "Xian, 'Elvis'], [ Kanishka' , .x1an. 'Alejandro,'Xian, 'Elvis', ['Elvis', 'Xian', Alejandro', 'Bridgette', 'Elvis', "Alejandro'], ['Xian', 'Bridgette [[ 'Alejandro', 'Xian', 'Elvis'), ['Elvis', .Xian. .Alejandro'], ['Bridgette', 'Elvis'' .ALejandro'], ["Xian', 'Bridgette', ['Kanishka'' Xian. 'Alejandro, 'Xian', 'Elvis', 'Elvis', 'Xian', Alejandro', 'Bridgette', 'Elvis, "Alejandro', ['xian', 'Bridgette', Elvis', 'Kanishka', "Xian' 'Alejandro, 'Xian, 'Elvis, 'Elvis',"Xian', Alejandro', 'Bridgette', 'Elvis, "Alejandro', ['Xian', 'Bridgette, Elvis', 'Kanishka', "Xian' 'Alejandro', 'Xian', Elvis' 1, ['Elvis', 'Xian', 'Alejandro, ['Bridgette','Elvis', 'Alejandro', 'Xian', 'Bridgette, 'Elvis'], 'Kanishka', "xian', [['Alejandro', .Xian', 'Elvis'), ['Elvis'' Xian. .Alejandro'], ['Bridgette', 'Elvis'' 'Alejandro'), ["Xian', 'Bridgette', 'Elvis'], ['Kanishka', 'Xian. 'Alejandro, 'Xian', 'Elvis', ['Elvis', Xian', 'Alejandro, 'Bridgette, 'Elvis, "Alejandro, ['Xian*,'Bridgette, 'Elvis , ['Kanishka', "Xian', 'Alejandro,'Xian, 'Elvis, 'Elvis',"Xian', Alejandro', 'Bridgette', 'Elvis', "Alejandro', ['Xian', 'Bridgette Elvis', Kanishka', "Xian', 'Alejandro', 'Xian', 'Elvis', ['Elvis', 'Xian', 'Alejandro' l, ['Bridgette'Elvis', Alejandro' ], ['xian', "Bridgette', 'Elvis', 'Kanishka', "'Xian', I Alejandro', 'Xian', 'Elvis', 'Elvis',xian,Alejandro, 'Bridgette',Elvis',Alejandro'], ['Xian', 'Bridgette', 'Elvis'1, ['Kanishka', 'xianStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started