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_versionA(L):
s = set()
d = dict()
for v in L:
s.add(v)
if (v in d):
d[v] += len(s)
else:
d[v] = v
print(f's = {s}')
print(f'd = {d}')
ct_versionA([1,2,1,1,5,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_versionA(d):
s = t = ''
for count in range(4):
t = d[t]
s += t
return s == 'yes'
3. Free Response [60 points]
Say we are given the following multiline string of animalData:
animalData = '''
species,breed,name
dog,labrador,fred
cat,persian,betty
dog,shepherd,barney
dog,labrador,fred
dog,labrador,wilma
'''
Write the function makeSpeciesDictionary(animalData) that takes data formatted
like this, and returns a dictionary mapping each species to another dictionary
that maps each breed of that species to a set of the names in that table
for that species.
For example, for the animalData given above, makeSpeciesDictionary(animalData)
returns this:
{
'dog':
{ 'labrador' : { 'fred', 'wilma' },
'shepherd' : { 'barney' }
},
'cat':
{ 'persian' : { 'betty' }
}
}
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.