Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

# Problem 1: # # Other types of Python container objects may have embedded data.

ID: 3836222 • Letter: #

Question

# Problem 1:
#
# Other types of Python container objects may have embedded data.
# For example, we could represent an appointment calendar as a
# dictionary of dictionaries. Each item in the outer dictionary
# represents a day of the week, and each dictionary object
# represents the appointments scheduled for that day.
# Your solution must be recursive, but may also contain loops.
# Here is an example:
#
# >>> monday = {'12:00' : 'Office hours', '1:30' : 'CSC 242 Lecture',
# '4:15' : 'Office hours'. '5:45' : CSC 406 Lecture'}
# >>> tuesday = { }
# >>> wednesday = {'12:00': 'Office hours', '1:30' : 'CSC 242 Lecture',
# '3:10' : 'CSC 242 Lab'}
# >>> thursday = { }
# >>> friday = {'9:00' : 'Faculty committee meetings all day'}
# >>> calendar= {'monday':monday,'tuesday':tuesday, wednesday':wednesday,
# >>> 'thursday':thursday, 'friday':friday}

# Write a function called actities. It is passed a dictionary called
# "calendar", whose structure is similar to the variable
# "calendar" above. Your function should be written recursively,
# so that calendar may contain any number of embeddeded dictionaries,
# at any level of embedding. The function should return a ***set*** of
# (non-dictionary) values stored in calendar. Although your solution must
# be recursive, it may also contain loops.
#
# Given the above value for the variable calendar, here is an example
# call to activities:
#
# >>> activities(calendar)
#{'Office hours', 'CSC 242 lecture', 'CSC 242 Lab', 'CSC 406 lecture'}
#
# As is always the case with sets, the ordering of the items in the set is
# unimportant; in fact, you have no control of its ordering.
#
# For this problem, you may not use the values method of the dict class.
###############################################################
def activities(x):
return set() # replace

Explanation / Answer

def activities(x):
if type(x) != dict:
return {x}
else:
retSet = set()
for key in x:
ret = activities(x[key])
retSet = retSet.union(ret)
return retSet

monday = {'12:00' : 'Office hours', '1:30' : 'CSC 242 Lecture',
'4:15' : 'Office hours', '5:45' : 'CSC 406 Lecture'}
tuesday = { }
wednesday = {'12:00': 'Office hours', '1:30' : 'CSC 242 Lecture',
'3:10' : 'CSC 242 Lab'}
thursday = { }
friday = {'9:00' : 'Faculty committee meetings all day'}
calendar= {'monday':monday,'tuesday':tuesday, 'wednesday':wednesday,
'thursday':thursday, 'friday':friday}
print(activities(calendar))

# code link: https://paste.ee/p/AeZq1