CS115 MT
CS115 MT
Important Notes:
• You may assume that all values are unique in the dictionary.
• You may assume that all values are of immutable types in the dictionary.
• The input dictionary should NOT be modified.
rev = {}
for key in dict1:
rev[dict1[key]] = key
return rev
a) Answer:
a = [3,2,3,4,0]
i = 0
while i < len(a):
a[a[i]] = i
i += 1 [4, 2, 1, 0, 0]
print(a)
b) Answer:
y = 3
m=0n=0
for m in range(y):
m=0n=1
for n in range(y):
m=0n=2
print('m =',m,'n =',n)
m=1n=0
y = 1
m=2n=0
c)
print(lst)
d)
e1 = tup[1][1:2]
e1 += e1
Answer:
e2 = tup[3][1:2]
e2 += e2
def isDelectable(n):
"""Assumes n is an int > 0
Returns True if n is a delectable number,
Returns False otherwise"""
numStr = str(n)
print(n)
for i in range(2, len(numStr)+1):
prefix = numStr[:i]
num = int(prefix)
if num % i != 0:
return False
return True
b) Using the isDelectable function from part a), write a function named
separateDelectables that takes a list of ints, L, and removes the delectable
numbers from L and returns a new list containing the delectable numbers from L.
Returns an empty list if there are no delectables in the input list.
Note: 0 points will be given if the function does not use the isDelectable function from
part a
def separateDelectables(L):
"""Assumes L is a list of positive ints
Removes delectable ints from L and
Returns those delectable ints in a new list"""
delectables = []
i = 0
while i < len(L):
if isDelectable(L[i]):
delectables.append(L.pop(i))
else:
i += 1
return delectables
Important Notes:
• Each line of the file contains start_city, end_city and the distance between the
start_city and end_city (in kms).
• You may assume that the start_city on a given line was the end_city from the
previous line (i.e. the next city on the route).
• The origin may not be the first city in the file, and the destination may not be the
last city.
• You should NOT use a nested loop in your solution.
Izmir,Usak,220
Usak,Polatli,292
Polatli,Ankara,80
Ankara,Kirikkale,78.6
Kirikkale,Corum,164
Corum,Samsun,168
Samsun,Ordu,148
Ordu,Giresun,47.8
Giresun,Trabzon,130