Compsci 101 Exam 2 Sec02 Fall 2017 Problem 1 Sec 2 ['S', 'A'] ['S', 'A', ['T', 'R']] ['G', 'U', 'C', 'B', 'W'] ['C', 'S', 'U'] ['C', 'U'] [4, 7, 8] [3, 4, 7, 8] [7, 8] set([8, 1]) # elements in any order is ok set([1, 2, 8, 9]) # elements in any order is ok set([9, 2]) # elements in any order is ok ['A', 'F', 'H', 'K'] ['A', 'F', 'H', 'K', 'P'] [2, 4, 4, 5, 7] [('A', 2), ('F', 4), ('H', 5), ('K', 4), ('P', 7)] Part B: [40, 10, 50, 70] # elements in any order is ok [1, 3, 2, 4] # elements in any order is ok 4 Problem 2 Sec 2 Part A: ['car', 'airplane', 'train', 'skates'] Part B: ['ci', 'eh', 'sh'] Part C: result = [word[:3] + word[:3] for word in phrase.split() if len(word)%3 == 0] ALT result = [word[0:3]*2 for word in phrase.split() if len(word)%3 == 0] Part D: def howMany(fruitlist): d = {} for item in fruitlist: [name, fruit] = item.split(":") if name not in d: d[name] = 0 d[name] += 1 return sorted(d.items()) Problem 3 Sec 2 Part A def setup(filename): f = open(filename) ans = [] for item in f: item = item.strip() alist = item.split(":") first = alist[:2] values = alist[2:] values = [int(num) for num in values] alist = first + values ans.append(alist) return ans Part B Sec 2: def offers(data, student): ans = [] for item in data: if student == item[1]: total = sum(item[2:]) ans.append( (total, item[0]) ) ans = sorted(ans) flip = [(t[1],t[0]) for t in ans] # swap two items in each tuple return flip Problem 4 Sec 2 Part A: Sec 2 def clubs(data): ans = [] for item in data: ans.append(item[0]) return sorted(set(ans)) Part 4B: Sec 2 # students in leadership level or higher in at least one club def studentsClubs(data, num): ansset = set([]) for item in data: student = item[1] lead = int(item[4]) if lead >= num: ansset.add(student) return sorted(ansset) Part 4C: Sec 2 # return those clubs in common def clubsCommon(data, clist): ans = set([]) for item in data: club = item[0] ans.add(club) common = ans & set(clist) return sorted(list(common)) # or return sorted(common) Part 4D: Sec 2 def dictNamesToMeetings(data): d = {} for item in data: club = item[0] person = item[1] meetings = int(item[3]) if person not in d: d[person] = [] d[person].append((club, meetings)) return d Part 4E: Sec 2 def nameMostMeetings(data): maxnum = 0 maxname = "" maxlist = [] d = dictNamesToMeetings(data) for (name,mlist) in d.items(): x = sum([num for (club,num) in mlist]) if x > maxnum: maxnum = x maxname = name maxlist = [club for (club,num) in mlist ] return (maxname, sorted(maxlist) ) Part 4F: Sec 2 # return the name of the student in the most clubs def studentMost(data): d = {} for item in data: stud = item[1] if stud not in d: ALT: if stud not in d: d[stud] = 0 d[stud] = 1 d[stud] += 1 else: d[stud] += 1 # find max maxstud = "" maxnum = 0 for (stud,num) in d.items(): if num > maxnum: maxstud = stud maxnum = num return maxstud ALT Soln def studentMost(data): d = {} for item in data: stud = item[1] if stud not in d: d[stud] = 1 else: d[stud] += 1 sortlist = sorted([(val,key) for (key,val) in d.items()]) # max is at the end of the sorted list return sortlist[-1][1]