CompSci 101 Sec 01 Spring 2025 Exam 3 Problem 1 ['car', 'dog', 'rug', 'top'] [('ant', 3), ('bee', 3), ('tiger', 5), ('worm', 4)] [54, 37, 21, 9] ['bee', 'fox', 'ant', 'lion', 'hyena'] [(11, 6, 3), (3, 17), (9, 23, 5)] [(6, 2, 8), (9, 4, 7), (3, 10, 5)] ['E', 'M', 'S'] ('E', [5, 0]) Problem 2 Part A QUESTION 1: 20 (the sum of the index 1 items in all the values, 3 + 9 + 8 = 20) QUESTION 2: Given the dictionary dict and the number num and that each value in the dictionary is a list, it adds up all the numbers that are in the numTH index position in all the lists, and returns that total. QUESTION 3: for key,value in dict.items(): amount = amount + value[num] OR for tup in dict.items(): amount = amount + tup[1][num] OR d = dict.items() amount = sum([value[num] for (key,value) in d]) Problem 2 Part B QUESTION 1: [('reach', 4), ('heart', 3), ('strip', 0), ('alive', 2)] QUESTION 2: Lines 4-7 count how many letters in word appear in someword. QUESTION 3: This function returns a list of tuples where each tuple is two items. The first item is a word from wordlist, and the second item is an integer that is the number of letters from word that are in the first item. Problem 2 Part C def ordering1(lista): listb = sorted(lista, key=lambda x: x[2]) listc = sorted(listb, key=lambda x: x[1], reverse=True) listd = sorted(listc, key=lambda x: x[0], reverse=True) return listd OR def ordering1(lista): listb = sorted(lista, key=lambda x: x[-1]) listc = sorted(listb, key=lambda x: x[1], reverse=True) listd = sorted(listc, key=lambda x: x[0], reverse=True) return listd NOTE: THE CODE BELOW DOES NOT WORK!!!! TRICKY!!! def ordering1a(lista): ## DOES NOT WORK, ONLY TAKE OFF 1 PT for this listb = sorted(lista, key=lambda x: (x[0],-x[1], x[2]), reverse=True) return listb ## Some numbers are out of order Problem 3 Part A def peopleWhoLikeRestaurant(datalist, restaurant): answer = [] for alist in datalist: if restaurant in alist[3]: answer.append(alist[0]) return sorted(answer) OR def peopleWhoLikeRestaurant(datalist, restaurant): answer = [] for alist in datalist: person = alist[0] rest_list = alist[3] if restaurant in rest_list: answer.append(person) return sorted(answer) OR def peopleWhoLikeRestaurant(datalist, restaurant): answer = set([]) for alist in datalist: if restaurant in alist[3]: answer.add(alist[0]) return sorted(answer) OR def peopleWhoLikeRestaurant(datalist, restaurant): answer = [alist[0] for alist in datalist if restaurant in alist[3]] return sorted(answer) Problem 3 Part B def favRestaurantsFromDorm(datalist, dorm): answerSet = set() for alist in datalist: dormInList = alist[2] if dormInList == dorm: for rest in alist[3]: answerSet.add(rest) return sorted(answerSet) OR def favRestaurantsFromDorm(datalist, dorm): answerSet = set() for alist in datalist: dormInList = alist[2] if dormInList == dorm: answerSet = answerSet | set(alist[3]) return sorted(answerSet) OR def favRestaurantsFromDorm(datalist, dorm): answer = [] for alist in datalist: dormInList = alist[2] if dormInList == dorm: for rest in alist[3]: answer.append(rest) return sorted(set(answer) OR def favRestaurantsFromDorm(datalist, dorm): answer = [] for alist in datalist: dormInList = alist[2] if dormInList == dorm: answer.extend(alist[3]) return sorted(set(answer) OR def favRestaurantsFromDorm(datalist, dorm): answer = [] for alist in datalist: dormInList = alist[2] if dormInList == dorm: answer = answer + alist[3] return sorted(set(answer) Problem 3 Part C def countRestaurantsPeopleLike(datalist): answer = [] for alist in datalist: answer.append((alist[0],len(alist[3]))) answer_sorted = sorted(answer, key = lambda x: (-x[1], x[0])) return answer_sorted OR def countRestaurantsPeopleLike(datalist): answer = [] for alist in datalist: name = alist[0] restaurants = alist[3] count = 0 for rest in restaurants: count += 1 answer.append((name,count)) answer_sorted = sorted(answer, key = lambda x: (-x[1], x[0])) return answer_sorted OR def countRestaurantsPeopleLike(datalist): answer = [] for alist in datalist: answer.append((alist[0],len(alist[3]))) answer2 = sorted(answer, key = lambda x: x[0]) answer3 = sorted(answer2, key = lambda x: x[1], reverse=True) return answer3 OR def countRestaurantsPeopleLike(datalist): d = {} for alist in datalist: d[alist[0]] = len(alist[3]) answer = sorted(d.items(), key = lambda x: (-x[1], x[0])) return answer Problem 3 Part D def topRestaurantMoreThanOnce(datalist): d = {} for alist in datalist: topRest = alist[3][0] if topRest not in d: d[topRest] = [alist[0]] else: d[topRest].append(alist[0]) answer = [key for (key,value) in d.items() if len(value) >= 2] return sorted(answer) OR def topRestaurantMoreThanOnce(datalist): lsta = [] for alist in datalist: lsta.append( alist[3][0]) lstb = [] for rest in set(lsta): if lsta.count(rest) >= 2: lstb.append(rest) return sorted(lstb) Problem 3 Part E def dictRestaurantToListRankings(datalist): d = {} for alist in datalist: rests = alist[3] index = 1 for rest in rests: if rest not in d: d[rest] = [index] else: d[rest].append(index) index = index + 1 return d OR def dictRestaurantToListRankings(datalist): d = {} for alist in datalist: rests = alist[3] index = 1 for rest in rests: if rest not in d: d[rest] = [] d[rest].append(index) index = index + 1 return d OR def dictRestaurantToListRankings(datalist): d = {} for alist in datalist: rests = alist[3] for index in range(len(rests)): rest = rests[index] if rest not in d: d[rest] = [] rank = index + 1 d[rest].append(rank) return d OR def dictRestaurantToListRankings(datalist): d = {} for alist in datalist: rests = alist[3] for rest in rests: rank = rests.index(rest) + 1 if rest not in d: d[rest] = [rank] else: d[rest] = d[rest] + [rank] return d Problem 3 Part F def restaurantWithTopScore(datalist): dictRatings = dictRestaurantToListRankings(datalist) maxRest = "" maxScore = 0 for rest,scores in dictRatings.items(): score = 0 for num in scores: if num == 1: score += 5 elif num == 2: score += 3 elif num == 3: score += 1 if score > maxScore: maxScore = score maxRest = rest return maxRest