CompSci 101 Fall 2020, Exam 2 Solutions Problem 2.1 Part A result = [w for w in food if w.count('a') == 1] OR result = [w for w in food if sum([1 for x in w if x=='a']) == 1] Problem 2.2 Part B result = [w for w in words if 'a' in w[:int(len(w)/2)]] OR print("a in first half", result) Problem 2.3 Part C result = [w for w in set(list1)&set(list2)] OR result = list(set(list1)&set(list2)) OR result = [w for w in set(list1) if w in set(list2)] OR result = list(set([w for w in list1 if w in list2])) OR result = list(set(list1).intersection(set(list2))) Problem 2.4 Part D result = list(set(list1)^set(list2)) OR result = [w for w in set(list1)^set(list2)] OR result = list(set(list1).symmetric_difference(set(list2))) OR result = list(set(list1)-set(list2)) + list(set(list2)-set(list1)) OR list(set([w for w in list1 if w not in list2]+[w for w in list2 if w not in list1])) Problem 2.5 Part E result = a[b.index(max(b))] OR result = [a[i] for i in range(len(a)) if b[i] == max(b)][0] OR result = [x for x in a if a.index(x) == b.index(max(b))][0] OR dict = {} for idx in range(len(b)): dict[a[idx]] = b[idx] final = sorted(dict.items(), key = lambda x:x[1]) result = final[-1][0] Problem 2.6 Part F result = sorted([x for x in d]) OR result = sorted(d.keys()) OR result = [a[0] for a in sorted(d.items(), key = lambda x:x[0])] Problem 2.7 Part G result = sorted(set([x for x in d.values()])) OR result = sorted([x for x in set(d.values())]) OR result = sorted(set(d.values())) OR result = [x for x in sorted(set(d.values()))] OR result = sorted(list(set(d.values()))) Problem 2.8 Part H result = sum([len(d[x]) for x in d.keys()]) OR result = sum([len(d[x]) for x in d]) OR result = sum([len(x) for x in d.values()]) Problem 3 Here is a correct solution def isAlpha(letter): return letter in "abcdefghijklmnopqrstuvwxyz" def processScores(data): answer = [] index = 0 name = "" nums = [] first = True while index 1: print("Error in " + k) OR d = {} for line in data: if line[0] not in d: d[line[0]] = [] d[line[0]].append(line[2]) for key in d: if len(set(d[key])) != 1: print('Error in price', key) OR dict = {} for x in data: if x[0] not in dict: dict[x[0]] = set([x[2]]) else: dict[x[0]].add(x[2]) list =[w[0] for w in dict.items() if len(w[1])>=2] for i in list: print('Error in ' + i) Problem 4.4 Part D def dictNamesToListAnimals(data): d = {} for lst in data: if lst[3] == "won": if lst[1] not in d: d[lst[1] ] = [] d[lst[1]].append(lst[4]) return d OR def dictNamesToListAnimals(data): d = {} for lst in data: if lst[1] not in d and lst[3] == 'won': d[lst[1]] = [lst[-1]] elif lst[1] in d and lst[3] == 'won'': d[lst[1]].append(lst[-1]) return d OR def dictNamesToListAnimals(data): d = {} for lst in data: if len(lst) == 5: if lst[1] not in d: d[lst[1]] = [lst[4]] else: d[lst[1]] += [lst[4]] # must be a list if adding to list return d OR def dictNamesToListAnimals(data): d = {} for lst in data: if len(lst) > 4: person = lst[1] animal = lst[-1] if person not in d: d[person] = [] d[person].append(animal) return d Problem 4.5 Part E def animalWon(dname, numtimes): d = {} for name in dname: for animal in dname[name]: if animal not in d: d[animal] = 0 d[animal] += 1 answer = [key for key in d if d[key] == numtimes ] return sorted(answer) OR def animalWon(dname, numtimes): d = {} for player in dname: for animal in dname[player]: if animal not in d: d[animal] = 0 d[animal] += 1 result = [x for x, y in d.items() if y == numtimes] return sorted(result) OR def animalWon(dname, numtimes): d = {} lst = [] for alist in dname.values(): for animal in alist: if animal not in d: d[animal] = 0 d[animal] += 1 for key in d: if d[key] == numtimes: lst.append(key) return sorted(lst) OR def animalWon(dname, numtimes): animals = [] for list in dname.values(): for animal in list: animals.append(animal) unique = set(animals) ret = [] for animal in unique: if animals.count(animal) == numtimes: ret.append(animal) return sorted(ret) OR def animalWon(dname, numtimes): ret = [] animals = dname.values() animal_lst = [] for element in animals: animal_lst += element # adding list to list animal_unique = set(animal_lst) for a in animal_unique: n = animal_lst.count(a) if n == numtimes: ret.append(a) return sorted(ret) OR def animalWon(dname, numtimes): lst = [] answer = [] for lst2 in dname.values(): lst += lst2 for item in lst: if lst.count(item) == numtimes and item not in answer: answer.append(item) return sorted(answer) Problem 4.6 Part F def wonMostPrizes(data): d= {} for lst in data: if lst[3] == "won": if lst[1] not in d: d[lst[1]] = [] d[lst[1]].append(lst[4]) print("dict built") maxnum = max([len(d[key]) for key in d]) answer = [(key, sorted(d[key])) for key in d if len(d[key]) == maxnum] return answer[0] OR def wonMostPrizes(data): d = dictNamesToListAnimals(data) maxnum = max([len(d[key]) for key in d]) answer = [(key, sorted(d[key])) for key in d if len(d[key]) == maxnum] return answer[0] OR def wonMostPrizes(data): d = dictNamesToListAnimals(data) high = 0 winner = '' for person in d: if len(d[person]) > high: high = len(d[person]) winner = person ret = (winner, sorted(d[winner])) return ret