---
title: Drill 13 Solution
tags: Drills-F20, 2020
---
# Drill 13 Solution
## Question 1
Given this dictionary, key_to_value,
```
key_to_val = {}
key_to_val["a"] = 100
key_to_val["a"] = 500
key_to_val["b"] = 20
```
How many keys are inside the dict?
( ) Error: You cannot use the same key twice in a dictionary.
( ) 1
( ) 3
( ) 2
::: spoiler Answer
2
When we define `key_to_val`, it has 0 keys. We add `"a"` and it gets one key, then we update the value of `"a"`, and it still has one key. We add `"b"` and it gets a second key.
:::
## Question 2
Suppose we have a list of names:
`names = ["Joe", "Bob", "Mary", "Joe", "Bob", "Bob"]`
Fill in the blank so that we create a dictionary that maps a
name to the number of times it appears in our list of names.
```
name_to_count = {} # our dict mapping names to count
for name in names:
if name in ___________:
name_to_count[name] = name_to_count[name] + 1
else:
name_to_count[name] = 1
```
::: spoiler Answer
name_to_count
:::
## Question 3
Given the following code:
```
dept = "CS"
prof_to_dept["Kathi"] = dept
prof_to_dept["Tim"] = "CS"
prof_to_dept["Bob"] = "English"
dept = "English"
```
What does `prof_to_dept["Kathi"]` evaluate to?
( ) "CS"
( ) "English"
( ) Error: Cannot change value in a dict.
::: spoiler Answer
"CS"
:::
## Question 4
Let's say we want to keep track of the courses that we're taking this semester at Brown.
```
student_to_courses = {}
leo_courses = ["CS111", "ENGL400"]
student_to_courses["Leo"] = leo_courses
student_to_courses["Naomi"] = ["CS111"]
student_to_courses["Jane"] = ["CS17"]
leo_courses.append("CS127")
```
What will the line of code `student_to_courses["Leo"]` evaluate to?
( ) `["CS111", "ENGL400"]`
( ) `["CS127", "CS111", "ENGL400"]`
( ) `["ENGL400", "CS127", "CS111"]`
( ) `["CS111", "ENGL400", "CS127"]`
:::spoiler Answer
`["CS111", "ENGL400", "CS127"]`
When `leo_courses` is updated, so is `student_to_courses["Leo"]`, because they are two names for the same list.
:::
## Question 5
Let's say we want to keep track of the number of fruits in a shopping cart.
```
cart = {}
grape = 2
cart["apple"] = 1
cart["grape"] = grape
cart["melon"] = 3
grape = 10
```
What will the line of code `cart["grape"]` evaluate to?
( ) 10
( ) 2
( ) 3
( ) error: cannot change the value in a dictionary
:::spoiler Answer
2
The key "grape" is initially mapped to a value of 2. Even though the name grape changes to 10, it does not change the original key-value mapping of grape to 2.
:::
## Question 6
Suppose we have a list of fruits:
```
fruits = ["apple", "apple", "banana", "apple"]
```
Will the following piece of code successfully create a dictionary that maps each fruit in fruits to the number of times it appears in the list?
```
fruit_to_count = {} # our dict mapping fruits to count
for fruit in fruits:
fruit_to_count[fruit] = 0
fruit_to_count[fruit] += 1
```
( ) Yes
( ) No
:::spoiler Answer
No
Because the code does not check whether a fruit already exists in the dictionary, it overrides each fruit value to 0 and increments it by 1 in each iteration of the loop.
:::
## Question 7
Assume we have a dictionary that maps student names to their heights. Fill in the blanks in the following function that returns the maximum height in students.
```
def get_max_student_height(students: dict):
max_height = 0
for BLANK1 in BLANK2:
max_height = max(max_height, students[name])
return max_height
print(get_max_student_height({"rohan" : 70, "leo": 75})
```
:::spoiler Answer
BLANK1: name
BLANK2: students
:::