CPS 101 Exam 2 Spring 2021 Solutions Problem 2.1 Part A The variable result should calculate the list of numbers from the list nums that are greater than 8. nums = [5, 2, 9, 21, 38, 6, 15, 4] result = [n for n in nums if n > 8] OR result = [n for n in nums if n >= 9] Problem 2.2 Part B The variable result should calculate the list of numbers from the list nums that are between 5 and 20. Note that does not include the numbers 5 or 20. nums = [5, 2, 9, 21, 38, 6, 15, 4] result = [x for x in nums if x > 5 and x < 20] OR result = [x for x in nums if x < 20 and x > 5] OR result = [x for x in nums if 5 < x < 20] # Yes this works in Python! OR result = [i for i in nums if 6 <= i <= 19] OR result = [num for num in nums if num in range(5+1,20)] OR result = [i for i in nums if i in range(6, 20)] Problem 2.3 Part C The variable result should create a list of the first letter from each word in the list colors. Assume each string in the list is one word that is lowercase. colors = ['blue', 'red', 'yellow', 'green', 'white', 'brown', 'purple'] result = [w[0] for w in colors] Problem 2.4 Part D The variable result should calculate the list of words from the list colors that end in a vowel. Vowels are defined as the letters "a", "e", "i", "o", and "u". Assume each word in the list colors is lowercase. You may also use the String variable vowels defined below. vowels = "aeiou" words = ['blue', 'red', 'yellow', 'aqua', 'green', 'white', 'purple'] result = [w for w in colors if w[-1] in vowels] OR result = [w for w in colors if w[-1] in "aeiou"] OR result = [x for x in colors if x[-1] == "a" or x[-1] == "e" or x[-1] == "I" or x[-1] == "o" or x[-1] == "u"] OR [w for w in colors if w[len(w)-1] in vowels] Problem 3.1 Part A The variable result should calculate the sum of those numbers in the list nums that are divisible by 3 and are even numbers. nums = [5, 18, 2, 9, 21, 1, 29, 22, 6, 15, 4] result = sum([n for n in nums if n%3 ==0 and n%2 == 0]) OR result = sum([n for n in nums if n%2 ==0 and n%3 == 0]) OR result = sum([n for n in nums if n%6 ==0]) OR result = sum( n for n in nums if n%6 ==0 ) # works without the []'s in sum OR result = sum([i for i in nums if i % 2 < 1 and i % 3 < 1]) OR result = [num for num in nums if num % 2 == 0 and num % 3 == 0] result = sum(result) OR div3 = [n for n in nums if n % 3 == 0] even = [k for k in div3 if k % 2 == 0] result = sum(even) OR sum = 0 for n in nums: if n%3 == 0: if n%2 == 0: sum += n result = sum OR sum = 0 for n in nums: if n%3 == 0 and n%2 == 0: sum += n result = sum OR sum = 0 for n in nums: if n%2 == 0 and n%3 == 0: sum += n result = sum OR goodNums = [] for x in nums: if x % 2 == 0 and x % 3 == 0: goodNums.append(x) sum = 0 for y in goodNums: sum = sum + y result = sum Problem 3.2 Part B phrase = "bug car bird top red boat ship tree eye blue" letter = 'b' result = "-".join([w[1:] for w in phrase.split() if w[0]==letter] ) OR lst = [] for w in phrase.split(): if w[0] == letter: lst.append(w[1:]) result = "-".join(lst) OR lst = [w[1:] for w in phrase.split() if w[0]==letter] result = "-".join(lst) OR lst = phrase.split() filt = [word for word in lst if word[0] == letter] cut = [k[1:] for k in filt] result = '-'.join(cut) OR new = [] result = "" phrase = phrase.split(" ") for w in phrase: if letter == w[0]: new.append(w[1:]) for i in range(len(new)): result += (new[i] + "-") return result[:-1] # This strips off the extra '-' Problem 4 1 def process(data): 2 sum = data[0] 3 index = 1 4 while (data[index] > data[index-1]): 5 sum += data[index] 6 index = index + 1 7 return sum A) process([1, 2, 4, 3]) Note the numbers in the list increase and then decrease B) process([1, 2, 3, 4]) Note the numbers in the list only increase. This crashes with an error C) The error is on line 4. index is updated to 4 and then on line 4 the comparison is data[4] > data[3] crashes since the list has positions 0 to 3, but no position 4 D) Change line 4 to: 4 while (index < len(data) and data[index] > data[index-1]): OR 4 while (index != len(data) and data[index] > data[index-1]): OR 4 while (index <= len(data)-1 and data[index] > data[index-1]): OR 4 while (index in range(len(data))) and data[index] > data[index-1] and Problem 5 A) def schoolOfPlayer(data, player): for lst in data: if lst[1] == player: return lst[0] return "None" OR def schoolOfPlayer(data, player): lst = [] for item in data: if item[1] == player: lst.append(item) if len(lst) >= 1: return lst[0][0] else: return "None" OR def schoolOfPlayer(data, player): ret = [] for lst in data: if lst[1] == player: ret.append(lst[0]) if len(ret) == 0: ret.append('None') return ret[0] Problem 5 B) def topPoints(data): maxpts = 0 maxnm = "None" for lst in data: if lst[2] > maxpts: maxpts = lst[2] maxnm = lst[1] + " at " + lst[0] return maxnm OR def topPoints(data): maxnm = '' maxpts = 0 for k in range(len(data)): if data[k][2] > maxpts: maxpts = data[k][2] maxnm = data[k][1] + ' at ' + data[k][0] return maxnm OR def topPoints(data): numlist = [] for lst in data: numlist.append(lst[2]) index = numlist.index(max(numlist)) return data[index][1] + " at " + data[index][0] Note this solution does not use an if, it is inside the max function! OR def topPoints(data): for lst in data: if lst[2] == max([n[2] for n in data]): return lst[1]+" at "+lst[0] OR def topPoints(data): max_value = 0 index = 0 for x in range (0,len(data)): if data[x][2] > max_value: max_value = data[x][2] index = x return data[index][1] + " at" + " " + data[index][0] Problem 5 C) def playersFromSchool(data, school): answer = [lst[1] for lst in data if lst[0] == school] return answer OR def playersFromSchool(data, school): answer = [] for lst in data: if lst[0] == school: answer.append(lst[1]) return answer OR def playersFromSchool(data, school): names = [] for i in range(len(data)): if data[i][0] == school: names += [data[i][1]] return names Problem 5 D) def topPlayers(data): answer = [] for lst in data: if lst[2] >= 10.0 and lst[3] >= 10.0: answer.append((lst[0],lst[1])) elif lst[2] >= 10.0 and lst[4] >= 10.0: answer.append((lst[0], lst[1])) elif lst[3] >= 10.0 and lst[4] >= 10.0: answer.append((lst[0], lst[1])) return answer OR def topPlayers(data): return [(lst[0],lst[1]) for lst in data if (lst[2] >= 10.0 and lst[3] >= 10.0) or (lst[2] >= 10.0 and lst[4] >= 10.0) or (lst[3] >= 10.0 and lst[4] >= 10.0) ] OR def topPlayers(data): ret = [] count = 0 for lst in data: if lst[2] >= 10: count += 1 if lst[3] >= 10: count += 1 if lst[4] >= 10: count += 1 if count >= 2: ret.append((lst[0], lst[1])) return ret OR def topPlayers(data): lst = [] for i in range(len(data)): avgs = [data[i][2], data[i][3], data[i][4]] overTen = [w for w in avgs if w >= 10.0] if len(overTen) >= 2: lst+= [ (data[i][0], data[i][1]) ] return lst OR def topPlayers(data): num=0 lst=[] for sublist in data: for i in range(2,5): if sublist[i] >= 10.0: num+=1 if num>=2: lst.append((sublist[0],sublist[1])) return lst Problem 5 E) def fileToList(filename): f = open(filename) answer = [] for line in f: parts = line.strip().split(":") part1 = parts[0].split(" ") school = " ".join(part1[:-1]) student = part1[-1] part2 = parts[1].split("-") for index in range(0,3): part2[index] = float(part2[index]) points = part2[0] rebounds = part2[1] assists = part2[2] answer.append([school, student, points, rebounds, assists]) f.close() return answer OR def fileToList(filename): f = open(filename) answer = [] for line in f: parts = line.strip().split(":") part1 = parts[0].split(" ") school = " ".join(part1[:-1]) student = part1[-1] part2 = parts[1].split("-") points = float(part2[0]) rebounds = float(part2[1]) assists = float(part2[2]) answer.append([school, student, points, rebounds, assists]) f.close() return answer OR - JUST THE BODY PART line = line.strip() lst = item.split(":") names = lst[0] names = names.split() name = names.pop() school = " ".join(names) avgs = lst[1].split("-") avgs = [float(n) for n in avgs] answer.append([school, name, avgs[0], avgs[1], avgs[2]]) OR - JUST BODY PART ret = [] strFlt= line.split(":") names = strFlt[0].split() ret.append(" ".join(names[:-1])) ret.append(names[-1]) floats = strFlt[1].split("-") ret.append(float(floats[0])) ret.append(float(floats[1])) ret.append(float(floats[2])) answer.append(ret)