CS 101 Fall 2025 Exam 3 SOLUTIONS ==================================================== Problem 1 ==================================================== Answer for Sec 01: ['abc', 'axy', 'bcd', 'bop'] [('h', 'head'), ('s', 'knees'), ('t', 'toes'), ('u', 'shoulders')] ['walrus', 'narwhal', 'hippo', 'elephant'] ['arm', 'seat', 'sofa', 'couch', 'chair'] [(2, 7), (8, 2, 5), (9, 1, 5)] [(6, 0, 5), (8, 1, 4), (6, 3, 5)] [(6, 0, 2), (8, 1, 4), (6, 0, 5)] (3, ['C', 'D']) Answer for Sec 02: ['gem', 'gif', 'hid', 'hop'] [('s', 'ears'), ('s', 'nose'), ('u', 'mouth'), ('y', 'eyes')] ['penguin', 'owl', 'bird', 'baluga'] ['pad', 'key', 'door', 'house', 'cabin'] [(6, 2, 7), (8, 2, 5), (9, 4)] [(6, 0, 5), (8, 1, 4), (6, 3, 5)] [(6, 0, 2), (8, 1, 4), (6, 0, 5)] (3, ['C', 'D']) ==================================================== Problem 2A ==================================================== QUESTION 1: { 7, 3 } QUESTION 2: Given dictionary `adict` and list `alist`, return a the set of keys of `adict` whose values are in `alist`. QUESTION 3: def mysteryA(adict, alist): ret = set() for key,val in adict.items(): if val in alist: ret.add(key) return ret OR def mysteryA(adict, alist): ret = [ key for key,val in adict.items() if val in alist ] return set(ret) ==================================================== Problem 2B ==================================================== QUESTION 1: [ 2, 3 ] # 'you' has all 3 in 'soundsystem', the others have only 2. QUESTION 2: Given a string `word` and string `sentence` containing words separated by single spaces, return a sorted list of integers where each integer is the number of unique characters in common between the [parameter `word` and each word in `sentence`. QUESTION 3: Replace the last line with: return sorted(ret, key=lambda x: (len(str(x)), -x)) OR ret = sorted(ret, reverse=True) ret = sorted(ret, key=lambda x: len(str(x))) ==================================================== Problem 2C ==================================================== return sorted(lista, key=lambda x: (-x[2], x[0], x[1])) OR ret = sorted(lista, key=lambda x: x[1]) ret = sorted(ret, key=lambda x: x[0]) ret = sorted(ret, key=lambda x: x[2], reverse=True) ==================================================== Problem 3A ==================================================== def beginsOrEndsWith(datalist, dept): count = 0 for row in datalist: if dept in [row[3][0], row[3][-1]]: count += 1 return count OR def beginsOrEndsWith(datalist, dept): satisfied = [ row for row in datalist if row[3][0] == dept or row[3][-1] == dept ] return len(satisfied) OR def beginsOrEndsWith(datalist, dept): return sum( [ row[3][0] == dept or row[3][-1] == dept for row in datalist ] ) # Adds Trues as 1, Falses as 0. ==================================================== Problem 3B ==================================================== def countMajorMatches(datalist): ret = [] for name, _, maj, sch in datalist: if maj == 'Undeclared': continue ret.append( (name, sch.count(maj)) ) return sorted(ret, key=lambda x: x[1]) OR def countMajorMatches(datalist): ret = [] for name, _, maj, sch in datalist: if maj != 'Undeclared': ret.append( (name, sch.count(maj)) ) return sorted(ret, key=lambda x: x[1]) OR def countMajorMatches(datalist): ret = [ (name, sch.count(maj)) for name, _, maj, sch in datalist if maj != 'Undeclared' ] return sorted(ret, key=lambda x: x[1]) ==================================================== Problem 3C ==================================================== def takenMostAt(datalist, num): d = {} for _, _, _, sch in datalist: if num < len(sch): c = sch[num] if c not in d: d[c] = 0 d[c] += 1 most = max(d.values()) deptsWithMost = [ k for k in d if d[k] == most ] return sorted(deptsWithMost) OR def takenMostAt(datalist, num): depts = [] for _, _, _, sch in datalist: if num < len(sch): c = sch[num] depts.append(c) uniquedepts = set(depts) maxdept = max(uniquedepts, key=lambda x: depts.count(x)) maxc = maxdept.count(maxdept) ties = [d for d in uniquedepts if depts.count(d) == maxc] return sorted(ties) OR def takenMostAt(datalist, num): depts = [ sch[num] for _, _, _, sch in datalist if len(sch) >= num ] uniquedepts = set(depts) maxdept = max(uniquedepts, key=lambda x: depts.count(x)) maxc = maxdept.count(maxdept) ties = [d for d in uniquedepts if depts.count(d) == maxc] return sorted(ties) ==================================================== Problem 3D ==================================================== def enrollmentsByKind(datalist): d = {} for _, _, maj, sch in datalist: for c in sch: if c not in d: d[c] = [0,0] if c == maj: d[c][0] += 1 else: d[c][1] += 1 return d OR def enrollmentsByKind(datalist): same_maj = {} diff_maj = {} for _, _, maj, sch in datalist: for c in sch: if c not in same_maj: same_maj[c] = 0 diff_maj[c] = 0 if c == maj: same_maj[c] += 1 else: diff_maj[c] += 1 d = {} for dept in set(same_maj): d[dept] = [ same_maj[dept], diff_maj[dept] ] return d ==================================================== Problem 3E ==================================================== def majorConnections(datalist, dept): d = {} for _, _, maj, sch in datalist: if maj == dept: for c in sch: if c not in d: d[c] = [0] d[c] += 1 return d OR def majorConnections(datalist, dept): courses = [] for _, _, maj, sch in datalist: if maj == dept: courses += sch uniquecourses = set(courses) d = {} for c in uniquecourses: d[c] = courses.count(c) return d