-
Notifications
You must be signed in to change notification settings - Fork 0
/
L19Q3_IsOffered.py
98 lines (82 loc) · 3.6 KB
/
L19Q3_IsOffered.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Dictionaries of Dictionaries (of Dictionaries)
# The next several questions concern the data structure below for keeping
# track of Udacity's courses (where all of the values are strings):
# { <hexamester>, { <class>: { <property>: <value>, ... },
# ... },
# ... }
#For example,
courses = {
'feb2012': { 'cs101': {'name': 'Building a Search Engine',
'teacher': 'Dave',
'assistant': 'Peter C.'},
'cs373': {'name': 'Programming a Robotic Car',
'teacher': 'Sebastian',
'assistant': 'Andy'}},
'apr2012': { 'cs101': {'name': 'Building a Search Engine',
'teacher': 'Dave',
'assistant': 'Sarah'},
'cs212': {'name': 'The Design of Computer Programs',
'teacher': 'Peter N.',
'assistant': 'Andy',
'prereq': 'cs101'},
'cs253':
{'name': 'Web Application Engineering - Building a Blog',
'teacher': 'Steve',
'prereq': 'cs101'},
'cs262':
{'name': 'Programming Languages - Building a Web Browser',
'teacher': 'Wes',
'assistant': 'Peter C.',
'prereq': 'cs101'},
'cs373': {'name': 'Programming a Robotic Car',
'teacher': 'Sebastian'},
'cs387': {'name': 'Applied Cryptography',
'teacher': 'Dave'}},
'jan2044': { 'cs001': {'name': 'Building a Quantum Holodeck',
'teacher': 'Dorina'},
'cs003': {'name': 'Programming a Robotic Robotics Teacher',
'teacher': 'Jasper'},
}
}
# If you want to loop through the keys in the dictionary,
# you can use the construct below.
# for <key> in <dictionary>:
# <block>
# For example, this procedure returns a list of all the courses offered
# in the given hexamester:
def courses_offered(courses, hexamester):
res = []
for c in courses[hexamester]:
res.append(c)
return res
# You do not need to use this code if you do not want to and may find another,
# simpler method to answer this question, although later ones may require this.
# Define a procedure, is_offered(courses, course, hexamester), that returns
# True if the input course is offered in the input hexamester, and returns
# False otherwise. For example,
# (Note: it is okay if your procedure produces an error if the input
# hexamester is not included in courses.
# For example, is_offered(courses, 'cs101', 'dec2011') can produce an error.)
# However, do not leave any uncommented statements in your code which lead
# to an error as your code will be graded as incorrect.
#my initial answer
def is_offered(courses, course, hexamester):
if hexamester in courses:
if course in courses[hexamester]:
return True
else:
return False
return 'hexamester not found'
#provided solution
def is_offeredUdacity(courses, course, hexamester):
return course in courses[hexamester]
print is_offered(courses, 'cs101', 'apr2012')
#>>> True
print is_offered(courses, 'cs253', 'feb2012')
#>>> False
print is_offered(courses, 'cs003', 'apr2012')
#>>> False
print is_offered(courses, 'cs001', 'jan2044')
#>>> True
#print is_offered(courses, 'cs101', 'apr2010')
#>>> error or hexamester not found