# 1, Part A 3 4 {0, 3, 4, 9, 10} # Order does not matter {0, 9} # Order does not matter ['b', 'l', 'u', 'e'] ['cookie', 'cake'] [3, 9, 15, 21] 4 3 True False [3, 4, 2] False ZeroDivisionError # Error is also correct # 1, Part B [1, 3, 4] [0, 1, 3, 4] [3, 6, 8] [3, 5, 6, 8] [3, 4, 7, 10] [3, 4, 7, 10] # 2, Part A ## any list that is NOT in order, returns False Call: check([1,3,2]) Return value: False # 2, Part B ## any list that is in order, returns Error, should return True Call: check([1,2,3]) Return value: Error Correct value: True # 2, Part C ## Solution 1, keep i = 0 def check(l): i = 0 # Fix 1: while i+1 < len(l) and l[i] < l[i+1]: # Fix 2: while i < len(l)-1 and l[i] < l[i+1]: while i < len(l) and l[i] < l[i+1]: i += 1 # Fix 1: if i+1 == len(l): # Fix 2: if i == len(l)-1: if i == len(l): return True else: return False ## Solution 2, make i = 1 def check(l): # Fix: i = 1 i = 0 # Fix: while i < len(l) and l[i-1] < l[i]: while i < len(l) and l[i] < l[i+1]: i += 1 if i == len(l): return True else: return False # 3 ## Solution 1, with sets def phraseCompare(phrase1, phrase2): s1 = set(''.join(phrase1.split())) s2 = set(''.join(phrase2.split())) return (len(s1 - s2), len(s1 & s2), len(s2 - s1)) ## Solution 2, with sets version 2 def phraseCompare(phrase1, phrase2): s1 = set(phrase1) s2 = set(phrase2) if ' ' in s1: s1.remove(' ') if ' ' in s2: s2.remove(' ') return (len(s1 - s2), len(s1 & s2), len(s2 - s1)) ## Solution 3, with simple counts def phraseCompare(phrase1, phrase2): left = 0 middle = 0 right = 0 p1 = ''.join(phrase1.split()) p2 = ''.join(phrase2.split()) for c in p1: if c in p2: middle += 1 else: left += 1 for c in p2: if c not in p1: right += 1 return (left, middle, right) ## Solution 4, with list comprehensions def phraseCompare(phrase1, phrase2): left = len([c for c in phrase1 if c != ' ' and c not in phrase2]) middle = len([c for c in phrase1 if c != ' ' and c in phrase2]) right = len([c for c in phrase2 if c != ' ' and c not in phrase1]) return (left, middle, right) # 4 ## Solution 1, with for loop def total(coins, values): total = 0 for c in coins: total += values[c] return total ## Solution 2, with list comprehension and using sum def total(coins, values): return sum([values[c] for c in coins]) # 5, Part A ## Solution 1, with list comprehension birds = [t[1] for t in pets if t[2] == 'bird'] ## Solution 2, with loop birds = [] for p in pets: if p[2] == 'bird': birds.append(p[1]) # 5, Part B ## Solution 1, if statements def compareDate(yearA, monthA, dayA, yearB, monthB, dayB): # All if...elif...else could be only ifs instead if yearA < yearB: return -1 elif yearA > yearB: return 1 else: if monthA < monthB: return -1 elif monthA > monthB: return 1 else: if dayA < dayB: return -1 elif dayA > dayB: return 1 else: return 0 ## Solution 2, using tuples (tuples could be a list too) def compareDate(yearA, monthA, dayA, yearB, monthB, dayB): a = (yearA, monthA, dayA) b = (yearB, monthB, dayB) if a < b: return -1 elif a > b: return 1 else: return 0 ## Solution 3, using complex boolean logic def compareDate(yearA, monthA, dayA, yearB, monthB, dayB): ret = 1 if (yearA <= yearB and monthA <= monthB and dayA <= dayB): ret = -1 if (yearA == yearB and monthA == monthB and dayA == dayB): ret = 0 return ret # 5, Part C ## Solution 1 def needCheckup(pets, appointments, year, month, day): for p in pets: lastApt = appointments[p[-1]][-1] # Or p[4] # Order does not matter as long as checking return value correctly if compareDate(year, month, day, lastApt[0], lastApt[1], lastApt[2]) == 1: lst.append((p[0], p[1])) return lst ## Solution 2, not as efficient but in 101 this doesn't matter as long as it works def needCheckup(pets, appointments, year, month, day): ids = [] for petId,appts in appointments.items(): lastApt = appts[-1] # Or p[4] if compareDate(year, month, day, lastApt[0], lastApt[1], lastApt[2]) == 1: ids.append(petId) ret = [] for pet in pets: if pet[-1] in ids: ret.append((pet[0], pet[1])) return ret # 5, Part D def usuallySick(pets, appointments): lst = [] for p in pets: sick = 0 checkup = 0 for a in appointments[p[-1]]: # Or p[4] # Since an appointment can also be "other" must specifically count # for 'sick' and 'checkup' if a[-1] == 'sick': # Or a[3] sick += 1 elif a[-1] == 'checkup': checkup += 1 if sick > checkup: # Should be strictly greater than lst.append(p) return lst