CompSci 101 Fall 2024 Exam 2 Solutions Problem 1 Part A: 5 [0, 1, 2, 3, 4] ['gary', 'pearl', 'sandy', 'spongeBob', 'squidward'] Error Part B: 3 "moe's tavern" Error marge&homer' Part C: {4,5,6} {1,2} {4,5,6,9,10} Problem 2 Multiple solutions possible for all parts. Part A: [x % 5 for x in values if x % 2 == 0] Part B: [(k, v) for k, v in var.items() if k > 8 or sum(v) < 10] Part C: sorted([x for x in set(nums2) - set(nums1)]) Part D: [word for word in words if len(word) > 3 and word.count('a') <= 2] Problem 3 Part A: Infinite loop. Part B: def build_count_dict(lst): # fixed version count_dict = {} i = 0 while i < len(lst): item = lst[i] # Error #1: missing default value if item not in count_dict and str(item).isalpha(): count_dict[item] = 0 if str(item).isalpha(): count_dict[item] += 1 # Error #2: missing updated i variable i += 1 return count_dict Problem 4 Multiple solutions possible. def calculate_total(purchases): total_spent = {} for purchase in purchases: item, quantity, price = purchase.split(":") quantity = int(quantity) price = float(price) if item in total_spent: total_spent[item] += quantity * price else: total_spent[item] = quantity * price return total_spent Problem 5 Multiple solutions possible. def filter_words(sentences): result = set() for sentence in sentences: words = sentence.split() for word in words: if len(word) > 5 and word[0] in 'aeiou': result.add(word) return sorted(result) Problem 6 Multiple solutions possible. def most_wins(records): if not records: return [] wins_dict = {} for record in records: school, scores = record.split(':') scores = scores.split(',') total_wins = 0 for score in scores: total_wins += int(score) if school not in wins_dict: wins_dict[school] = 0 wins_dict[school] += total_wins result = list(wins_dict.items()) return result