Unit 2

Dictionaries

Code Reading

[14.1] What will the following evaluate to?

d = {"movies":3, "songs":2}

a. d["movies"]
b. d["songs"]
c. d
d. d[3]
e. "mango" in d
f. "movies" in d

[14.2] What will the value of d be after each change, namely what will be printed at each step?

d = {}
d["math"] = 90

print("change 1: ", d)

d["english"] = 87
print("change 2: ", d)

d["french"] = d["math"] + 4
print("change 3: ", d)

d["english"] = d["english"] - 3
print("change 4: ", d)

[14.3] What will the following code print?

my_dict = {
    "name": "John",
    "age": 25,
    "city": "New York",
    "is_student": False
}

for p in my_dict:
    print("key: ", p, " had value ", my_dict[p])

[14.4] Trace the following code. What happens in each line?


1. d = { 26: "Chen", 23: "Patrick" }
2. d[88] = "Rosa"
3. d[23] = "Pat"
4. d[51] = d[23]
5. if "Chen" in d:
6.      d.pop("Chen")

[14.5] What does the following code do?


def mystery(d):
 total = 0
 for key in d:
    total = total + d[key]
 return total

Code Writing

[14.6] Write a line of code to do the following:


a. make an empty dictionary (call it d1)
b. make a dictionary mapping strings to integers (call it d2)
c. print the length of the d2
d. add a new key-value pair to d2
e. update a key-value pair in d2
f. remove a key-value pair in d2

[14.7] Write a function that takes a string as input and returns a dictionary with the count of each word in the string.

[14.8] Create a function that inverts the keys and values of a given dictionary.

[14.9] Write a function that takes two dictionaries of the same size as input and returns a new dictionary containing only the common key-value pairs.

[14.10] Write a function that merges two dictionaries, summing the values for common keys.

[14.11] Create a function that searches for a key based on a given value in a dictionary. The function returns the key if it finds it or None otherwise. Assume the values are unique.

[14.12] Write a function that given a dictionary of string-number pairs, create a new dictionary that includes only the pairs where the number values are negative. ssume the number values could be either negative or positive.

[14.13] Create a function countChar that takes in a string and returns a dictionary that maps the frequency of how many times a character appears. Treat lowercase and uppercase chars as the same

[14.14] Given a dictionary of strings to numbers, multiply all the numbers and return the value in the end.

[14.15] Given a list of words, write a function to find the most frequently occurring word and return its count. Use a dictionary to store the frequenices, then iterate the dictionary to find the maxiumum frequency.

[14.16] Write a program that takes a dictionary mapping strings to numbers and a limit (a number) and returns True if all the keys are at most the limit in length, and False otherwise.

For example, hasShortKeys({ "abc" : 2, "de" : 5}, 3) would return True, but hasShortKeys({ "abc" : 2, "defgh": 2}, 4) would return False.

[14.17] Write a function called makeAlphabetDict that will create an alphabet ditionary for a list of strings. For each starting letter, create a new list if the letter has not been seen before, or add to the exisiting list if it has been seen.

For example when called with the following list

makeAlphabetDict(["hello", "area", "he", "me", "bye", "my"])

will return the following dictionary:

{'h': ['hello', 'he'], 'a': ['area'], 'm': ['me', 'my'], 'b': ['bye']}