CompSci 101 Fall 2020 Test 1 Solutions Problem 1 (2 pts) Honor Code Problem 2 PART 2A (3 pts) phrase = 'The quick brown fox jumps over the lazy dog' result = phrase[2] + phrase[-1] + phrase[-5] OR result = phrase[28] + phrase[42] + phrase[38] OR result = phrase[33] + phrase[42] + phrase[38] PART 2B (3 pts) lst = ['computer'] result = lst[0][1] + lst[0][-1] OR result = lst[0][1] + lst[0][7] PART 2C (3 pts) phrase = 'DukeRoad' result = phrase[-2:] + phrase[2:4] OR result = phrase[6:] + phrase[2:4] OR result = phrase[6:8] + phrase[2:4] PART 2D (3 pts) lst = [['dog', 'cat'], ['ant', 'fox']] result = lst[0][1] + lst[1][0] OR result = lst[0][-1] + lst[-1][0] PART 2E (3 pts) lst = [['elephant', 'fox','lion'], ['giraffe', 'zebra']] result = lst[0][1][-1] + lst[1][1][0] OR result = lst[0][1][2] + lst[1][1][0] PART 2F (3 pts) lst = [['monkey', 'armadillo','turtle'], ['snake', 'dolphin', 'crab']] result = len(lst[1][0]) PART 2G (3 pts) phrase = 'beekeeper' result = 'o'.join(phrase.split('e')) OR "o".join("oo".join(phrase.split("ee")).split("e")) OR result = "oo".join(phrase.split("ee")) Replace ee's first result = "o".join(result.split("e")) Then replace o's PART 2H (4 pts) phrase = "monkey say monkey see monkey hear monkey do" result1 = "".join(phrase.split("monkey")) result = "*".join(result1.split()) OR result1 = "*".join(phrase.split(" monkey ")) result = "".join(result1.split("monkey ")) OR result1 = "*".join(phrase.split()) result = "".join(result1.split("monkey*")) PART 2I (3 pts) num = 2346 result = num//10%200 OR result = num//10%100 OR result = num//69 OR result = num/69 The result could be 34 or 34.0 Problem 3A (7 points) def tshirtShop(price, numOrders, spent): if spent + price < 500.00: return price if numOrders > 50: price = price * .80 elif numOrders > 20: price = price * .90 return price OR def tshirtShop(price, numOrders, spent): if spent + price < 500.00: return price if numOrders > 50: return price * .80 if numOrders > 20: return price * .90 return price OR def tshirtShop(price, numOrders, spent): if spent + price < 500.00: return price if numOrders > 50: return price - (price * 0.2) if numOrders > 20: return price - (price * 0.1) return price OR def tshirtShop(price, numOrders, spent): if (spent + price) >= 500: if numOrders > 50: return price * 0.8 if numOrders > 20: return price * 0.9 else: return price return price # notice this return is needed Problem 3B (7 points) def theCount(phrase,let1, let2): count = 0 for word in phrase.split(): if word[0]==let1 and word[-1]==let2: count += 1 return count OR def theCount(phrase,let1, let2): answer = [] lst = phrase.split() for word in lst: if word[0]==let1 and word[-1]==let2: answer.append(word) return len(answer) OR def theCount(phrase,let1, let2): answer = [] for word in phrase.split(): if word[0]==let1 and word[-1]==let2: answer += [1] return len(answer) Problem 4A (6 pts) def isNumber(word): for ch in word: if ch not in '0123456789': return False return True a) any string in a different format than "56a" that results in a wrong answer. For example any string of that has a number first and at least one letter will return True when it should return false. "34ab" b) line 5 Should say the function is returning after the first iteration of the for loop. Thus, it is not checking all the characters in word. c) line 5 should be de-indented, it should line up with the for. Problem 4B Mystery (6 pts) a) (2 pts ) "1 2 3 4" OR "1 a 4 b" Every other word starting with the first word must be an integer b) "yes no 6 8" If any of the 1st, 3rd, 5th, etc word is not a number there is an error c) It turns every other word in the string starting with the first word into an integer and returns the sum of those integers. Problem 5 A) (8 pts) def process(line): lst = line.split(":") first = lst[0].split() dish = " ".join(first[1:]) second = lst[1].split("-") return [first[0], dish, int(second[0]), float(second[1])] OR def process(line): answer = [ ] lst = line.split(":") first = lst[0].split() answer.append(first[0]) # or answer += [first[0]] answer.append(" ".join(first[1:])) second = lst[1].split("-") answer.append(int(second[0])) answer.append(float(second[1])) return answer OR def process(line): answer = [ ] lst = line.split(":") first = lst[0].split() answer += [first[0]] answer += [" ".join(first[1:])] second = lst[1].split("-") answer += [ int(second[0]) ] answer += [ float(second[1]) ] return answer OR def process(line): lst1 = line.split(':') pos = lst1[0].find(' ') type = lst1[0][:pos] name = lst1[0][(pos+1):] lst2 = lst1[1].split('-') cal = int(lst2[0]) cost = float(lst2[1]) return [type, name, cal, cost] # OR return [type] + [name] + [cal] + [cost] Problem 5 B) (8 pts) def formatList(lst): answer = [] for item in lst: answer.append(process(item)) return answer OR def formatList(lst): answer = [] for item in lst: answer += [process(item)] return answer Problem 5 C) (8 pts) def maxOfType(lst,type): maxItem = "" maxValue = 0 for item in lst: if item[0]== type and item[2] > maxValue: maxValue = item[2] maxItem = item[1] return maxItem OR def maxOfType(lst,type): values = [] names = [] for item in lst: if item[0]== type: values.append(item[2]) names.append(item[1]) maxvalue = max(values) pos = values.index(maxvalue) return names[pos] OR def maxOfType(lst,type): maxValue = 0 for item in lst: if item[0]== type and item[2] > maxValue: maxValue = item[2] for item in lst: if item[2] == maxValue: return item[1] return maxItem