# Nested Dictionaries and Iterators in Python Homework Sample

Write a function that takes a dictionary of bus routes, each with a list of stops. Create a reverse dictionary using the stops as key, and a list of all the bus routes that stop there (in alphabetical order). Create a function that takes a dictionary of days, and each day has a dictionary of course name and time spent studying the course. Sum the total time spent studying each course name. Create a function to search a list of dictionaries to find the key if present, and the value. Create a function subsets, which takes a list and returns all the sets contained within (so subsets([1,2]) returns [], [1], [2], [1, 2]). For even more Python assignment help contact us for a quote.

Solution:

debugging = True
def debug(*s):
if debugging:
print(*s)

def busStops(b):
result = {}
for key in b:
for value in b[key]:
if value in result.keys():
result[value].append(key)
else:
result[value] = [key]
return result

result = {}
for key in d:
for dict in d[key]:
value = d[key].get(dict)
if dict in result.keys():
result[dict] = result[dict] + value
else:
result[dict] = value
return result

result = {}
for element in L:
for key in element:
for dict in element[key]:
value = element[key].get(dict)
if dict in result.keys():
result[dict] = result[dict] + value
else:
result[dict] = value
return result

def searchDicts(L,k):
for dict in reversed(L):
if k in dict.keys():
return dict[k]
return None

def searchDicts2(tL,k):
next,dict = tL[-1]
if k in dict.keys():
return dict[k]
elif len(tL) == 1:
return None
else:
return searchDicts2(tL[0:next+1],k)

def subsets(s):
sets = []
for i in range(2**len(s)):
subset = []
for j in range(len(s)):
if i & (1 << j) > 0:
subset.append(s[j])
sets.append(subset)
return sets

def numPaths(m, n):
if(m == 1 or n == 1):
return 1
return numPaths(m-1, n) + numPaths(m, n-1)

class iterPrimes():
num = 1

def __next__(self):
num = self.num +1
for val in range(num, 2*num):
if val == 2:
self.num = val
break
isPrime = True
for i in range(2, val):
if val % i == 0:
isPrime = False
break
if isPrime:
self.num = val
break
return self.num

def numbersToSum(iNumbers,sum):
list = []
num = iNumbers.num
sum = sum – num
while(sum > 0):
if num != 1:
list.append(num)
num = iNumbers.__next__()
sum = sum – num
return list

def testbusStops():
buses = {
‘Lentil’: [‘Chinook’, ‘Orchard’, ‘Valley’, ‘Emerald’,’Providence’, ‘Stadium’, ‘Main’, ‘Arbor’, ‘Sunnyside’, ‘Fountain’, ‘Crestview’, ‘Wheatland’, ‘Walmart’, ‘Bishop’, ‘Derby’, ‘Dilke’],
‘Wheat’: [‘Chinook’, ‘Orchard’, ‘Valley’, ‘Maple’,’Aspen’, ‘TerreView’, ‘Clay’, ‘Dismores’, ‘Martin’, ‘Bishop’, ‘Walmart’, ‘PorchLight’, ‘Campus’],
‘Silver’: [‘TransferStation’, ‘PorchLight’, ‘Stadium’, ‘Bishop’,’Walmart’, ‘Shopco’, ‘RockeyWay’],
‘Blue’: [‘TransferStation’, ‘State’, ‘Larry’, ‘TerreView’,’Grand’, ‘TacoBell’, ‘Chinook’, ‘Library’],
}
result = busStops(buses)

test = {‘Chinook’: [‘Lentil’, ‘Wheat’, ‘Blue’], ‘Orchard’: [‘Lentil’, ‘Wheat’], ‘Valley’: [‘Lentil’, ‘Wheat’], ‘Emerald’: [‘Lentil’], ‘Providence’: [‘Lentil’], ‘Stadium’: [‘Lentil’, ‘Silver’, ‘Gray’], ‘Main’: [‘Lentil’, ‘Gray’], ‘Arbor’: [‘Lentil’], ‘Sunnyside’: [‘Lentil’, ‘Gray’], ‘Fountain’: [‘Lentil’], ‘Crestview’: [‘Lentil’, ‘Gray’], ‘Wheatland’: [‘Lentil’], ‘Walmart’: [‘Lentil’, ‘Wheat’, ‘Silver’], ‘Bishop’: [‘Lentil’, ‘Wheat’, ‘Silver’], ‘Derby’: [‘Lentil’], ‘Dilke’: [‘Lentil’], ‘Maple’: [‘Wheat’], ‘Aspen’: [‘Wheat’], ‘TerreView’: [‘Wheat’, ‘Blue’], ‘Clay’: [‘Wheat’], ‘Dismores’: [‘Wheat’], ‘Martin’: [‘Wheat’], ‘PorchLight’: [‘Wheat’, ‘Silver’], ‘Campus’: [‘Wheat’], ‘TransferStation’: [‘Silver’, ‘Blue’, ‘Gray’], ‘Shopco’: [‘Silver’], ‘RockeyWay’: [‘Silver’], ‘State’: [‘Blue’], ‘Larry’: [‘Blue’], ‘Grand’: [‘Blue’], ‘TacoBell’: [‘Blue’], ‘Library’: [‘Blue’], ‘Wawawai’: [‘Gray’], ‘CityHall’: [‘Gray’], ‘Colorado’: [‘Gray’]}
if result == test:
return True
return False

d = {‘Mon’:{‘355′:2,’451′:1,’360′:2},’Tue’:{‘451’:2, ‘360’:3}, ‘Thu’:{‘355′:3,’451′:2,’360’:3}, ‘Fri’:{‘355’:2}, ‘Sun’:{‘355′:1,’451′:3,’360’:1}}
return False
if addDict(d) != {‘355’: 8, ‘360’: 9, ‘451’: 8}:
return False
return True

log = [{‘Mon’:{‘355′:2,’360′:2},’Tue’:{‘451′:2,’360′:3},’Thu’:{‘360’:3}, ‘Fri’:{‘355’:2}, ‘Sun’:{‘355’:1}},
{‘Tue’:{‘360′:2},’Wed’:{‘355′:2},’Fri’:{‘360’:3, ‘355’:1}},
{‘Mon’:{‘360′:5},’Wed’:{‘451′:4},’Thu’:{‘355′:3},’Fri’:{‘360’:6}, ‘Sun’:{‘355’:5}}]
if addDictN(log) == {‘355’: 16, ‘360’: 24, ‘451’: 6}:
return True
return False

def testsearchDicts():
L1 = [{“x”:1,”y”:True,”z”:”found”},{“x”:2},{“y”:False}]

if searchDicts(L1,”x”) != 2:
return False
if searchDicts(L1,”y”) != False:
return False
if searchDicts(L1,”z”) != “found”:
return False
if searchDicts(L1,”t”) != None:
return False
return True

def testsearchDicts2():
L2 = [(0,{“x”:0,”y”:True,”z”:”zero”}),
(0,{“x”:1}),
(1,{“y”:False}),
(1,{“x”:3, “z”:”three”}),
(2,{})]
if searchDicts2 (L2,”x”) != 1:
return False
if searchDicts2 (L2,”y”) != False:
return False
if searchDicts2 (L2,”z”) != “zero”:
return False
if searchDicts2 (L2,”t”) != None:
return False
return True

def testsubsets():
if subsets ([1,2,3]) != [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]:
return False
if subsets([(1,”one”),(2,”two”)]) != [[],[(1,”one”)],[(2,”two”)],[(1,”one”),(2,”two”)]]:
return False
if subsets([]) != [[]]:
return False
return True

def testnumPaths():
if numPaths(2,2) != 2:
return False
if numPaths(3,3) != 6:
return False
if numPaths(4,5) != 35:
return False
return True

def testiterPrimes():
primes = iterPrimes()
if primes.__next__() != 2:
return False
if primes.__next__() != 3:
return False
if primes.__next__() != 5:
return False
return True

def testnumbersToSum():
primes = iterPrimes()
if numbersToSum(primes, 58) != [2, 3, 5, 7, 11, 13]:
return False
if numbersToSum(primes, 100) != [17, 19, 23, 29]:
return False
return True