CompSci 101 Spring 2017 Exam 1 Sec 02 Problem 1, 24 pts total CompSci ne bluewhite 4.0 4 True ue YELLOW nine 2 tele -1 boaaa5 15 ------------------------------------------------------------ Problem 2, 8 points def lunchPrice(item1, item2, day, month): high = max(item1, item2) low = min(item1,item2) if day=="Thursday": low = low - low*0.1 if day=="Friday": low = low - low*0.5 cost = low + high if month.startswith("M") or month.startswith("J"): cost = cost - 5 if cost < 0: cost = 0 return cost ALT Soln: def lunchPrice(item1, item2, day, month): if item1 > item2: if day=="Thursday": item2 = item2 * 0.9 if day=="Friday": item2 = item2 * 0.5 else: # item2 >= item1 if day=="Thursday": item1 = item1 * 0.9 if day=="Friday": item1 = item1 * 0.5 cost = item1 + item2 if month.startswith("M") or month.startswith("J"): cost = cost - 5 if cost < 0: cost = 0 return cost ALT SOLN: def lunchPrice(item1, item2, day, month): cost = item1 + item2 discount = 0 loweritem = item1 if item1 > item2: loweritem = item2 if day=="Thursday": discount = discount + 0.1 * loweritem if day=="Friday": discount += 0.5 * loweritem if month.startswith("M") or month.startswith("J"): discount = discount + 5 cost = cost - discount if cost < 0: cost = 0 return cost ------------------------------------------------------------ Problem 3 Part A ( 2 pts each) Q1. (2 pts) ['apple', 'honeydew', 'avocado'] Q2. 2 pts ['apple'] Q3. This function builds a new list of words from wordlist where each word either starts with the given letter ch or is of length greater than value. Those words are in the same order they are in from the original list. Problem 3 Part B (2 pts each) Q1. The code resets pos everytime it finds small again in phrase, to the index location of the most recent find. Thus it returns the last location that is found. Q2. Change line 6 to return i Q3. It returns -1 Q4. Change line 4 to ch = phrase[i:i+len(small)] ------------------------------------------------------------ Problem 4 Part A def allIn(word,letters): cnt = 0 for let in letters: if let in word: cnt+= 1 if cnt == len(letters): return True return False ALT Soln: def allIn2(word,letters): for let in letters: if let not in word: return False return True Problem 4 Part B def message(phrase, code): words = phrase.split() answer = "" for word in words: if allIn(word,code): answer += word[0] else: answer += word[-1] return answer ------------------------------------------------------------ Problem 5 Part A def fileToList(filename): answer = [] f = open(filename) for line in f: line = line.strip() line = line.split(":") namerating = line[0].split() rating = int(namerating[-1]) name = " ".join(namerating[:-1]) resAmount = line[1].split("$") amount = float(resAmount[1]) rest = resAmount[0] answer.append([name, rating, rest, amount]) return answer ALT SOLN: def fileToList(filename): answer = [] f = open(filename) for line in f: line = line.strip() line = line.split("$") resAmount = float(line[1]) line = line[0].split(":") rest = line[-1] # restaurant name line = line[0].split() rating = int(line[-1]) name = " ".join(line[:-1]) answer.append([name, rating, rest, resAmount]) return answer ALT SOLN: def dataToList(filename): answer = [] f = open(filename) for line in f: line = line.strip() line = line.replace("$", ":") # replace one separator with the other line = line.split(":") # splits everything but name and rating nameRating = line[0] lastBlank = nameRating.rfind(" ") # find the last blank name = nameRating[:lastBlank] rating = int(nameRating[lastBlank+1:]) rest = line[1] # restaurant name resAmount = float(line[2]) answer.append([name, rating, rest, resAmount]) return answer Problem 5 Part B def restaurantsEatenAt(datalist, name): ans = [] for list in datalist: if list[0] == name: if list[2] not in ans: ans.append(list[2]) return ans ALT SOLN: def restaurantsEatenAt(datalist, name): ans = [] for list in datalist: if list[0] == name and list[2] not in ans: ans.append(list[2]) return ans Problem 5 Part C def dinerWhoSpentTheMost(datalist): maxAmount = 0.0 maxdiner = "" for list in datalist: if list[-1] > maxAmount: maxAmount = list[-1] maxdiner = list[0] return maxdiner ALT SOLN: def dinerWhoSpentTheMost(datalist): spent = [] for list in datalist: spent.append(list[-1]) maxAmount = max(spent) maxdiner = "" for list in datalist: if list[-1] == maxAmount: # found max amount maxdiner = list[0] # found diner who spent most return maxdiner return maxdiner # Should never execute this line