CompSci 101 Sec 01, Fall 2014, Exam 2 Solutions Problem 1 ['do', 'fa', 'me', 're', 'so'] ['la', 'me', 'la'] ['la', 'me', 'la', 'do'] set([8, 2, 6]) set([2, 4, 6, 8, 10, 13]) set([13]) B ['Duke', 'ECU', 'UNC', 'WFU'] set(['Y', 'P', 'B']) ['B', 'G', 'Y'] Problem 2 A. [24, 6, 12] [5, 8] 5 B. startsWithA = [x for x in words if x[0].lower() == 'a'] OR startsWithA = [x for x in words if x[0] == 'a' or x[0] == 'A'] C. firstThree = [item[0:3] for item in words if len(item) > 5] OR firstThree = [item[:3] for item in words if len(item) > 5] Problem 3 A. def fileToList(fileMeals): answer = [] for line in fileMeals: line = line.strip() answer.append(line.split(":")) return answer OR return [line.strip().split(":") for line in fileMeals] B. def foodEaten(data, name): food = [] for item in data: fullname = item[2].split(" ") if fullname[-1] == name: food.append(item[0]) return food OR def foodEaten(data, name): return [item[0] for item in data if item[2].split()[-1] == name] Problem 4 A. def listOfSchools(data): setSchools = set([]) for item in data: setSchools.add(item[0]) alist = list(setSchools) alist.sort() return alist OR def listOfSchools(data): listSchools = [] for item in data: listSchools.append(item[0]) alist = list(set(listSchools)) alist.sort() return alist OR def listOfSchools(data): listSchools = [] for item in data: if item[0] not in listSchools: listSchools.append(item[0]) return sorted(listSchools) OR def listOfSchools(data): return sorted(list(set([item[0] for item in data]))) B. def problemsAttempted(data): problems = set([]) for item in data: problems.add(item[1]) return list(problems) OR def problemsAttempted(data): attempted = [] for item in data: if item[1] not in attempted: attempted.append(item[1]) return attempted C. def problemsNotAttempted(problems, data): attempted = problemsAttempted(data) setProbs = set(problems) setAttempted = set(attempted) setNotAttempted = setProbs.difference(setAttempted) return list(setNotAttempted) OR def problemsNotAttempted(problems, data): setProbs = set(problems) setAttempted = set(problemsAttempted(data)) setNotAttempted = setProbs - setAttempted return list(setNotAttempted) OR def problemsNotAttempted(problems, data): notAttempted = [] attempted = problemsAttempted(data) for pr in problems: if pr not in attempted: notAttempted.append(pr) return notAttempted D. def dictProblemsToSchoolsSolved(data): d = {} for item in data: if item[3] == 'correct': if item[1] in d: # already in d[item[1]].append(item[0]) else: # not in yet, add d[item[1]] = [item[0]] return d E. def dictSchoolsToNumSubmissions(data): d = {} for item in data: if item[0] in d: d[item[0]] += 1 else: d[item[0]] = 1 return d F. def easiestProblem(data): d = dictProblemsToSchoolsSolved(data) maxProb = ('',[]) for (key,value) in d.items(): if len(value) > len(maxProb[1]): maxProb = (key, value) return maxProb OR def easiestProblem(data): d = dictProblemsToSchoolsSolved(data) maxSize = max([len(d[key]) for key in d ]) for (key,value) in d.items(): if len(value) == maxSize: return (key, value) return ("", [])