Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Python 3 To represent a voting system, you will need to write multiple functions. You have starter code for each of them. Here are

Using Python 3

To represent a voting system, you will need to write multiple functions. You have starter code for each of them.

Here are the functions that you will need to implement for each system.

  • single_vote_X(candidates, preferences)
  • aggregate_votes_X(vote_list), where X will be replaced by the name of the appropriate voting system.

Each single_vote function takes in two arguments: a list of the candidates' names and a list with the utility for each candidate (floating-point numbers), and calculates the vote that this individual voter will cast by returning the name of the candidate under the requested voting system. If the number of candidates doesn't match the number of preferences, the function should return None.

Each aggregate_votes function takes in as an argument a list of all voters' votes (i.e., a list of names). The vote will be different types depending on the system, but whatever type it is, a list of all the votes is provided to the aggregate_votes function, which must calculate the true winner of the election according to the voting system. Again, the method of counting will vary, but the return value should be a single string, representing the winning candidate. Hint: A dictionary would make it straightforward for you to keep track of the score for each candidate. Using dictionary's .get method would help determine if the value has already been added.

If a tie occurs (i.e., more than one candidate has the same scoreumber of votes), then pay attention to the instruction on how to resolve them.image text in transcribed

Input Voting Data The voting data that you will need for this project will be provided to you in a file. The first line of the file will be the comma-separated names of all candidates, e.g., "Yoda, Yaddle, Grogu". The second line of the file will be an integer, which gives the size of the voting population, e.g., "576". Every subsequent line of the file will represent one individual in the voting population, and will be a series of whitespace-separated numbers which correspond to the utility for each candidate in order, e.g., "1 2 42" (in this example, for "Yoda Yaddle Grogu" respectively). A higher number represents a higher preference for a candidate For example, the voting data could be stored in a file "population.txt" with the following contents: Yoda, Yaddle, Grogu 1 1 2 42 or, to follow our other example, James Kirk, Kathryn Janeway 1 4.5 9.2 or, to use a more complicated example Jessie, Eli, Ava 4 11 12 10 14 3 9.5 7 9 8.2 16 2 20.9 Write a function called process_votes_file that takes the following parameters: a string representing the filename, which you can pass directly to the file open function a list of strings representing candidate names (without any leading/trailing whitespace) a nested list (i.e., a list of lists), where each list element inside of the main list represents one individual voter's numerical ranking of all candidates (i.e., a list of numbers, not a list of strings)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago