In this lab, you will use lists and list

  

In this lab, you will use lists and list operations to process election results and to declare a winner or announce a runoff. The vote information (number of votes each candidate receives in each district) must be read from a comma-delimited file named votes.csv. An example of the data and its format is given below: 31,509,156,21 75,190,417,143 402,80,265,0 298,212,130,176 234,678,97,127 102,78,62,116 The data above, shown in the bold outline below, should be placed into a two-dimensional list named votes. The list is interpreted as follows (shaded headers are not part of the data that will be read from the file, and are shown, along with the shaded italicized totals, for information only):

The program will then process the vote count, and once the data is tabulated, it should announce the election results. There are two possible outcomes:

Runoff – In the event that none of the candidates gets 51% of the vote (the example above, in file votes1.csv is such a case), a runoff between the top two vote-getting candidates is declared through the following output (note the decreasing order of vote-getters in both categories): No candidate attained 51% of the 4,599 votes cast, and a runoff will be held between: Candidate5 received 1,136 votes (24.70%) Candidate2 received 825 votes (17.94%) Other candidate vote totals: Candidate4 received 816 votes (17.54%) Candidate3 received 747 votes (16.24%) Candidate1 received 717 votes (15.59%) Candidate6 received 358 votes (7.78%)

Clear winner – If the top vote-getter wins more than 51% of the total vote, that candidate is declared the winner. Totals for other candidates are presented in decreasing order of votes received, as shown in the following output (example based on file votes2.csv): Candidate5 won the election with 2,936 votes out of 4,599 cast (63.84%) Candidate2 received 439 votes (9.55%) Candidate4 received 416 votes (9.05%) Candidate6 received 346 votes (7.52%) Candidate3 received 247 votes (5.37%) Candidate1 received 215 votes (4.67%)

Requirements: You must observe the following requirements in designing and developing your program:

You must read the file and load the votes list in the main() function.·

You must have a function get_vote_totals() that accepts the two-dimensional votes list, and returns a two-dimensional list that you will store in the variable vote_totals. This list has the candidate number at column index 0, and that candidate’s vote total at column index 1. Using the votes1.csv example data above (shaded headers are for information only and are not part of the list), the list would be:·

You will then pass the vote_totals list to the function sort_it(), provided for you below. This function will return the list sorted, in descending order, by the value at index position 1 (the total number of votes). Store the returned value in list variable sorted_votes. Using the data above, this sorted list returned is:·Write the remainder of the program so that it provides the election results output shown above.·

You may not use any global variables (global constants are permitted)·

Your program will be tested with multiple input files. It must handle both the Clear Winner and Runoff cases.·

data from votes1.csv:

data from votes2.csv:

Tags: No tags