CompSci 101 Sec 02 Spring 2025 Exam 2 Solutions Problem 1 SEC 02 SOLUTION GIVEN: {2, 5, 7} [2, 5, 7] [2, 5, 7] [1, 4, 5, 8] [3, 6] [1, 2, 5] [6, 7] [6] ['B', 'G', 'S', 'W'] ['G', 'W'] ['B', 'G', 'K', 'S', 'W'] [1, 2, 5, 6, 6] Problem 2 Part A result = [w for w in lst if 'et' in w[1:-1] ] OR result = [w for w in lst if 'et' in w[1:len(w)-1] ] OR result = [w for w in lst if 'et' in w and w[:2] != 'et' and w[-2:] != 'et'] OR result = [w for w in lst if 'et' in w and not w.startswith('et') and not w.endswith('et')] Problem 2 Part B result = [num*2 for num in lst if len(str(num)) == 2] OR result = [num*2 for num in lst if num>9 and num<100] OR result = [num*2 for num in lst if num>=10 and num<=99] OR result = [num*2 for num in lst if (num/10) >= 1 and (num/10) < 10] Problem 2 Part C result = [w for w in lst if w[-1]=='t' and (len(w)==2 or len(w)>5)] OR result = [w for w in lst if (len(w)==2 or len(w)>5) and w[-1]=='t'] OR result = [w for w in list if x.endswith('t') and (len(w)==2 or len(w)>5) ] Problem 3 Part A result = [(w[0],w[-1]+w[1:]) for w in phrase.split()] OR result = [] for w in phrase.split(): result.append((w[0],w[-1]+w[1:])) OR result = [] words = phrase.split() for w in words: newword = w[-1] + w[1:] result.append((w[0], newword) OR result = [] words = phrase.split() for w in words: if len(w) == 1: ans = (w,w) else: ans = (w[0], w[-1] + w[1:]) result.append(ans) Problem 3 Part B result = [courses[index] for index in range(len(courses)) if size[index] > 140] OR result = [] for index in range(len(courses)): if size[index] > 140: result.append(courses[index]) OR result = [] index = 0 for item in courses: if size[index] > 140: result.append(courses[index]) index += 1 OR result = [] index = 0 while index < len(courses): if size[index] > 140: result.append(courses[index]) index += 1 Problem 3 Part C result = sorted(set(phrase1.split()) ^ set(phrase2.split())) OR result = sorted(set(phrase1.split()).symmetric_difference(set(phrase2.split()))) OR set1 = set(phrase1.split()) set2 = set(phrase2.split()) result = sorted(set1 ^ set2) OR set1 = set(phrase1.split()) set2 = set(phrase2.split()) result = sorted(set1.symmetric_difference(set2)) OR result = [] for w in set(phrase1.split()): if w not in phrase2.split(): result.append(w) for w in set(phrase2.split()): if w not in phrase1.split(): result.append(w) result = sorted(result) Problem 4 Part A Problem 4 Part A def numberOrders(datalist, store): for storeInfo in datalist: if store == storeInfo[0]: #store matches return sum(storeInfo[-1]) return 0 OR def numberOrders(datalist, store): total = 0 for storeInfo in datalist: if store == storeInfo[0]: #store matches total += storeInfo[-1] return total OR #Note there is only one item in the list and you index into it to return that item. def numberOrders(datalist, store): return [sum(alist[-1]) for storeInfo in datalist if storeInfo[0] == store ][0] Problem 4 Part B def storesFromState(datalist, state): answer = [] for storeInfo in datalist: if state == storeInfo[1]: # state matches answer.append(storeInfo[0]) return sorted(answer) OR def storesFromState(datalist, state): answer = [storeInfo[0] for storeInfo in datalist if state == storeInfo[1] ] return sorted(answer) Problem 4 Part C def largestTotal(datalist): maxTotal = 0 maxPurch = "" for storeInfo in datalist: total = storeInfo[2]* sum(storeInfo[3]) if total > maxTotal: maxTotal = total maxPurch = storeInfo[0] return maxPurch Problem 4 Part D def missingStores(datalist,storesWant): haveset = set() for lst in datalist: haveset.add(lst[0]) wantset = set(storesWant) missing = wantset - haveset return sorted(missing) OR def missingStores(datalist,storesWant): storelist = [lst[0] for lst in datalist] storeset = set(storelist) wantset = set(storesWant) return sorted(wantset - storeset) Problem 4 Part E def processFile(filename): f = open(filename) datalist = [] for line in f: line = line.strip() alist = line.split(":") blist = alist[-1].split("-") blistInts = [int(x) for x in blist] finalList = [alist[0], alist[1], float(alist[2]), blistInts] datalist.append(finalList) return datalist