CompSci 101 Fall 2017 Test 1 Problem 1 Sec 02 t 6 t2 mint 5 t of pe 2catdog c-i-a-o False ['grape', 'cherry'] 3.3 1 Problem 2 Sec 02 Part A def calculate(price, days, percent): daysPercent = (days * percent)/100.00 calcPrice = price - (price * daysPercent) if calcPrice < 0.0: return 0.0 return calcPrice ALT SOLN def calculate(price, days, percent): calcPrice = price - (price * days * percent)/100.0 if calcPrice < 0.0: return 0.0 return calcPrice ALT SOLN def calculate(price, days, percent): calcPrice = price * (1 - (days * percent/100.0)) if calcPrice < 0.0: return 0.0 return calcPrice Part B (8 pts) def yardage(stfield, styard, efield, eyard): if stfield == efield: # same if styard > eyard: return styard - eyard else: return eyard - styard else: return 50 -styard + 50 - eyard Problem 3 Sec 02 PART A - Mystery (6 pts total) Q1. (2 pts) ['horse', 'gerbil', 'cow', 'raccoon', 'pig'] Q2. (1 pt) ['cow'] Q3. (1 pt) ['gerbil', 'cow'] Q4. (2 pts) This function creates a new list of the words by processing each word in the list and then deciding where to add it to the new list. For a word, if the first letter comes before "m" in alphabetical order, the word is appended to the left side of the new list. Otherwise the word is appended to the right side of the new list. PART B - Debugging (6 pts total) Q1. 2 pts Once we have counted the third vowel, we iterate to the next character in the word. Then we see we have reached the correct count and we find the position of the current character, which is one past the correct character. Q2. 1 pt findNthVowel("string", 1) returns 4 and should return 3. findNthVowel("mississippi", 3) returns 2 Q3. 1 pt findNthVowel("book", 1) returns 1 Q4. 2 pts def findNthVowel(word, num): # correct count = 0 vpos = -1 # add variable to store the postition for index in range(len(word)): ch = word[index] if count == num: return vpos # return the saved index position if isVowel(ch): count += 1 vpos = index # save the index position return -1 Problem 4 Sec 02 def reformat (name, num): alist = name.split() last = " ".join(alist[-1*num:]) first = alist[0] middle = "" if len(alist) > num+1: # yes middle names middle = alist[1:-1*num] lets = "" for word in middle: lets += word[0] if len(lets) > 0: return last + ", " + first + " " + lets else: return last + ", " + first Problem 5 Sec 02, Problem A (6 pts) def getParts(line): pos = line.find(" (") name = line[:pos] pos2 = line.find(")") sizestr = line[pos+2:pos2] size = float(sizestr) rest = line[pos2+1:].split() cals = int(rest[0]) type = " ".join(rest[1:]) return [name, size, cals, type] Problem 5 Sec 02, Problem B (4 pts) def fileToList(filename): answer = [] f = open(filename) # code below is answer for line in f: line = line.strip() answer.append(getParts(line)) # code below was given return answer Problem 5 Sec 02, Problem C (8 pts) def categories(snklist): answer = [] for alist in snklist: if alist[3] not in answer: answer.append(alist[3]) return answer Problem 5 Sec 02, Problem D (10 pts) def fewestCaloriesPerOz(snklist): first = snklist[0] snack = first[0] fewest = first[2]/first[1] for alist in snklist: calperoz = alist[2]/alist[1] if calperoz < fewest: fewest = calperoz snack = alist[0] return snack