Question: Upload an original Python script that satisfies the following specifications: It contains a function read_scores() that takes one argument (a path to a file like
Upload an original Python script that satisfies the following specifications:
- It contains a function read_scores() that takes one argument (a path to a file like votes.txt
) and returns a dictionary where each key is the name of a candidate and each value is a list of integers representing scores assigned to that candidate by voters. Do not hard-code a file path into this function. Assume that each line in the file consists of scores assigned by a single voter; assume that the scores are separated by commas and that each score consists of a candidate's name and a number between 0 and 9. It is not necessarily the case that every voter will assign a score to every candidate. - Below is an example of what lines in the file may look like:
Candidate 1:2,Candidate 2:3,Candidate 3:0,Candidate 4:9 Candidate 1:5,Candidate 3:9,Candidate 4:6 Candidate 2:2,Candidate 3:7,Candidate 4:4
- If the lines above were the entire contents of the file, the resulting dictionary would be:
{'Candidate 1': [2, 5], 'Candidate 2': [3, 2], 'Candidate 3': [0, 9, 7], 'Candidate 4': [9, 6, 4]}
- Below is an example of what lines in the file may look like:
- It contains a function calculate_results() that takes one argument (a dictionary as returned by read_scores()) and returns a dictionary where each key is a candidate and each value is a float which is the average of the scores assigned to that candidate.
- If the argument to this function were the dictionary above, the resulting dictionary would be:
{'Candidate 1': 3.5, 'Candidate 2': 2.5, 'Candidate 3': 5.333333333, 'Candidate 4': 6.333333333}
- If the argument to this function were the dictionary above, the resulting dictionary would be:
- It contains a function get_winner() that takes one argument (a dictionary as returned by calculate_results()) and returns the name of the candidate with the highest average score. Assume there will be a single winner (e.g., no tiebreaker is necessary, no multiple winners).
- If the argument to this function were the dictionary shown as an example under calculate_results() above, the resulting value would be 'Candidate 4' because Candidate 4 has the highest average score.
- It contains an if __name__ == '__main__': statement that gets one command line argument (a path to a file containing votes), calls the functions defined in the script, and prints to the console the name of the winner from the votes in the file containing votes.
- It does not use the input() method.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
