Date: Tuesday, November 21, 2023.
Due Date: Tuesday, November 28, 2023.
Goal: to use the dictionary data structure in Python.
Create a Python file called lab12.py and add your name and the lab number as a comment at the top.
In this exercise, we will work on the problem discussed in the lecture. We start with a dictionary containing judges in a contest as keys and who they voted for, or contestants, as values. The goal is to reverse the dictionary of votes by creating another dictionary indexed by the contestants, where we count how many votes each contestant had. Then we can use this new dictionary to find out the winner and loser, meaning the contestant with the largest and the smallest number of votes.
a. Dictionary reversal
Write a function countVotes(votes) that receives a dictionary indexed by judges' names and having the contestant they voted for as value. The function should create a new dictionary indexed by the contestants' names where for each of them, we count the votes that they have. The function should return this new dictionary.
Write a main function where you create a dictionary of votes with at least 10 judges. Then call the function countVotes in it and output the result.
b. Min and max values
Write two functions keyMaxVal(D) and keyMinVal(D) that receive a dictionary of votes indexed by the contestants (any dictionary in fact) and returns the key that has the largest and smallest values associated with them respectively.
In the main, add a call to each of these functions. Then use the results to output which is the winner and loser of the contest for that week. You may output that the winner can sing an extra song, and the loser needs to remove his/her mask.
c. Majority winner or top two
Write another function majorityWinner(D) that receives the same dictionary, and checks if the winner has a number of votes that represent at least half or more of the total number of votes. If that is the case, it should return True, and if not, return False.
Write another function secondBest(D) that returns the key with the second largest value in the dictionary. You can use the function keyMaxValue if it helps.
Add a test for these functions in the main with the dictionary of user names. First call the function majorityWinner, and if it returns True, output that the key with the largest value is a majority winner. If not, then call the function secondBest, and output that the key with the largest value and the second best need to go head to head for another round.
Upload the file lab12.py in Canvas, Assignments, Homework 12. You can wait until you finish the homework to upload all the files at the same time.