1. Code Tracing [20 points]
What does the following code print?
Be certain to show your work,
and also very clearly circle your answer!
def ct_versionC(L):
s = set()
d = dict()
for v in L:
s.add(v)
if (v in d):
d[v] += min(s)
else:
d[v] = v
print(f's = {s}')
print(f'd = {d}')
ct_versionC([8,8,4,8,4,2])
2. Reasoning Over Code [20 points]
What input makes the following function return True?
Be certain to show your work,
and also very clearly circle your answer!
def rc_versionC(d):
s = t = 0
for count in range(4):
t = d[t]
s += t
return s == 123
3. Free Response [60 points]
Say we are given the following multiline string of foodData:
foodData = '''
name,category,flavor
apple,fruit,sweet
broccoli,vegetable,neutral
turnip,vegetable,bitter
melon,fruit,sweet
plantain,fruit,neutral
'''
Write the function makeFoodDictionary(foodData) that takes data formatted
like this, and returns a dictionary mapping each category (i.e. fruit,
vegetable, etc) to another dictionary that maps each observed flavor
(i.e. we have seen bitter or neutral) to a set of the names of foods.
For example, for the foodData given above, makeFoodDictionary(foodData)
returns this:
{
'fruit':
{'sweet': {'melon', 'apple'},
'neutral': {'plantain'}
},
'vegetable':
{'neutral': {'broccoli'},
'bitter': {'turnip'}
}
}
Hint: note that the table begins with a blank line and then the
line of column labels. Also, the table ends with a blank line.