CompSci 101 Fall 2017 Test 1 Problem 1 Sec 01 f 4 ng data 4 nts 5 c end adogcat b-u-s False ['for', 'list'] 7.2 3 Problem 2 Sec 01 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 01 PART A - Mystery (6 pts total) Q1. (2 pts) ['strike', 'street', 'fish', 'tigers', 'eels'] Q2. (1 pt) ['fish'] Q3. (1 pt) ['fish', 'tigers'] 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, it gets the position of the two letters let1 and let2 in that word. If let2 is in a higher numbered position than let1, then the word is appended to the right side of the new list. Otherwise the word is appended to the left side of the new list. PART B - Debugging (6 pts total) Q1. 2 pts After the for loop, if exactly one vowel was found, the code searches for the position of the "last" character in the word and returns that position. That is not necessarily the position of the vowel. Q2. 1 pt Any word with one vowel where the vowel is not the last letter, such as: bark, stork, up Q3. 1 pt Vowel needs to be the last letter. the, to, pro, be Q4. 2 pts Whenever a vowel is counted, you also need to save the vowel in a variable. Then after the loop, if there is just one vowel, you can use that variable with find to find the location of that one vowel and return that location. def onlyOneVowel(word): count = 0 vowel = "" #create a variable to save the vowel for ch in word: if isVowel(ch): count += 1 vowel = ch # save the vowel if count == 1: return word.find(vowel) # look for the vowel. else: return -1 ALT SOLN: def onlyOneVowel(word): count = 0 indexSave = -1 # save the index for index in range(len(word)): #loop over indices ch = word[index] # extract the character to look at if isVowel(ch): count += 1 indexSave = index # save the index if count == 1: return indexSave # return the index, don't need to find it else: return -1 Problem 4 Sec 01 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 01, 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 01, 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 01, Problem C (8 pts) def snacksOfType(snklist, type, amount): ans = [] for alist in snklist: if alist[-1] == type: if alist[2] <= amount: ans.append(alist[0]) return ans Problem 5 Sec 01, Problem D (10 pts) def mostCalories(snklist, type): maxitem = "" maxcal = 0 for alist in snklist: if alist[3] == type: if alist[2] > maxcal: maxcal = alist[2] maxitem = alist[0] return maxitem ALT SOLN def mostCalories(snklist, type): typelist = [] for alist in snklist: if alist[3] == type: typelist.append(alist) maxitem = "" maxcal = 0 for alist in typelist: if alist[2] > maxcal: maxcal = alist[2] maxitem = alist[0] return maxitem ALT SOLN def mostCalories(snklist, type): typelist = [] for alist in snklist: if alist[3] == type: typelist.append(alist) maxcallist = [] for alist in typelist: maxcallist.append(alist[2]) maxcal = max(maxcallist) maxitem = "" for alist in typelist: if alist[2] == maxcal: # found it maxitem = alist[0] return maxitem