CompSci 101 Spring 2017 Exam 1 Sec 01 Problem 1, 24 pts total a CompSci b ho c fishdog d 2.0 e 2 f True g tu h CAT i knight j 5 yogi k -1 m co$a$5 n 12 ------------------------------------------------------------ Problem 2, 8 points def dinnerPrice(item1, item2, day, month): high = max(item1, item2) low = min(item1,item2) if day=="Tuesday": low = low - low*0.1 if day=="Monday": low = low - low*0.5 cost = low + high if month == "April" or month == "May": cost = cost - 5 if cost < 0: cost = 0 return cost ALT Soln: def dinnerPrice(item1, item2, day, month): if item1 > item2: if day=="Tuesday": item2 = item2 - item2*0.1 if day=="Monday": item2 = item2 - item2*0.5 else: # item2 >= item1 if day=="Tuesday": item1 = item1 - item1*0.1 if day=="Monday": item1 = item1 - item1*0.5 cost = item1 + item2 if month == "April" or month == "May": cost = cost - 5 if cost < 0: cost = 0 return cost ALT Soln: def dinnerPrice(item1, item2, day, month): cost = item1 + item2 discount = 0 if item1 > item2: loweritem = item2 else: loweritem = item1 if day=="Tuesday": discount += loweritem * 0.1 if day=="Monday": discount += loweritem * 0.5 if month == "April" or month == "May": 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 dinersForRestaurant(datalist, restname): ans = [] for list in datalist: if list[2] == restname: if list[0] not in ans: ans.append(list[0]) return ans ALT SOLN: def dinersForRestaurant(datalist, restname): ans = [] for list in datalist: if list[2] == restname and list[0] not in ans: ans.append(list[0]) return ans Problem 5 Part C def restWithHighestRating(datalist): bestrest = "" bestrating = 0 for list in datalist: if list[1] > bestrating: bestrating = list[1] bestrest = list[2] return bestrest ALT SOLN: def restWithHighestRating(datalist): ratings = [] for list in datalist: ratings.append(list[1]) best = max(ratings) print best for list in datalist: if list[1] == best: # found max return list[2] return "ERROR" # should never do this, don't need this line, but ok to have