Quiz 2 Dict Practice


Hi everyone! Here is a list of practice questions to help you review dicts and their uses. This is also a great review for your upcoming quiz. The answer key will be released later this week - be sure to try them first before looking at the answers!

Basketball Team

You have been hired as the coach of a basketball team. As the new coach, you have several tasks ahead of you and you are tring to figure out how to proceed. Luckily, you know Python and have access to historical team data that can help you make the right choices!

Tasks to Complete:

Average Scores of Players

Given a Dict with it’s key as players’ names and a corresponding List containing their scores over several games (Dict[str, List[int]]), find the average score of each player and return it as a dictionary. Expected function signature:

Example usage:

>>> find_avg_score({"kate": [10, 20, 30]})
                {"kate": 20}

Explanation:

Kate’s scores over 3 games = [10, 20, 30]; the average of that list is 20.

Joining Salary Data

Given two dictionaries: one containing the id and name of each player (Dict[int, str]) and the other containing their id and salary (Dict[int, int]), return a Dict containing the player’s name matched with their salary. Expected function signature:

Example usage:

>>>join_salary_data({30: "armando"}, {30: 10000}) 
                "armando: 10000"}

Explanation:

As given by first Dict, player with id = 30 is "armando". The second Dict tells us that player with id = 30 makes 10000 dollars. Therefore, "armando" makes 10000 dollars.

Note:

If you cannot find a player’s salary from the first dictionary with {id: name}, mark their salary in the output Dict as -1.

>>>join_salary_data({30: "armando", 20: "kate"}, {30: 10000})
                {"armando": 10000, "kate": -1}

Highest and Lowest Scores

Given the player names and average scores for all players on your team (Dict[str, int]), find the players with the highest and lowest scores. Return your result as a Dict containing the names of both players. Expected function signature:

Example Usage:

>>> highest_and_lowest({"kate": 20, "mary": 40, "john": 10}) 
                {"max": "mary", "min": "john"}

Compare Scores

As one of the best coaches in the game, you value consistency over one-time achievement! Given a Dict containing a players name and their corresponding scores for games in the season (Dict[str, List[int]]), find the player who consistenly had the highest score on the team. Expected function signature:

Example usage:

>>> compare_scores({"kate": [10, 20, 20], "john": [5, 7, 60]}, 3) 
                "kate"

Explanation:

Despite the fact that John’s total score (5 + 7 + 60 = 72) was higher than Kate’s total score (10 + 20 + 20 = 50), Kate scored higher in game 1 with a score of 10 vs. John’s 5 and in game 2 with a score of 20 vs. John’s 7. Thus, Kate had the highest score for 2 games while John had the highest score for only 1 game and "kate" is returned by the function.

Note: the parameter num_games tells you the number of games played by the team. Thus, the size of each List[int] in the dictionary will be equal to num_games.

Tests for Each of these Functions to Try Out!

Average Scores of Players

Joining Salary Data

Highest and Lowest Scores

Comparing Scores

Solutions to Each Problem

Average Scores of Players

Joining Salary Data

Highest and Lowest Scores

Comparing Scores