-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing-dicts.py
51 lines (38 loc) · 1.13 KB
/
testing-dicts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# namesages.py
# Stub file for lab 9, problem 1
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8
NAMES = ["Alice", "Bob", "Cathy", "Dan", "Ed", "Frank",
"Gary", "Helen", "Irene", "Jack", "Kelly", "Larry"]
AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
# Put code here that will combine these lists into a dictionary
ages_to_names = {}
for i in range(len(NAMES)):
name = NAMES[i]
age = AGES[i]
#print name
#print age
print ages_to_names
if age in ages_to_names:
ages_to_names[age].append(name)
else:
ages_to_names[age] = [name] # the LIST containing the name
# You can rename this function
def people(age):
""" Return the names of all the people who are the given age. """
if age in ages_to_names:
return ages_to_names[age]
return []
# Testing
#print people(18) == ["Cathy", "Dan"]
#print people(19) == ["Ed", "Helen", "Irene", "Jack", "Larry"]
#print people(20) == ["Alice", "Frank", "Gary"]
#print people(21) == ["Bob"]
#print people(22) == ["Kelly"]
#print people(23) == []
print people(21)
print NAMES[1]
print NAMES[5]
print ages_to_names
print ages_to_names[19]