CompSci 101 Fall 2016 - Test 1 Sec 02 Problem 1 24 pts (1 pt for type, 1 pt for value) yes ! play 3 13.0 4 r GREEN no! oOh False moybo 3 Problem 2 Part A (6 pts) # amount and cost are floats, discount is integer def howMany(amount, discount, cost): cost = cost - cost * discount/100 return int(amount/cost) ALT: def howMany(amount, discount, cost): cost = cost * (1 - discount/100.0) return int(amount/cost) Part B (8 pts) def carCost(price, day, numDays): if day % 7 == 0: price = price - price*0.10 if numDays - day < 5: price = price - 1000.00 return price ALT SOLN: def carCost(price, day, numDays): if day == '7' or day == "14" or day == '21' or day == 28: price = price*0.90 if numDays - day < 5: price = price - 1000.00 return price Problem 3 Sec 02 Part A (6 pts) def mystery(words, item): answer = [] count = 0 for w in words: newword = w if count % 2 == 0: newword = item answer.append(newword) count += 1 return answer Q1. ['peach', 'fig', 'peach', 'lemon', 'peach'] Q2. 'apple' Q3. Replaces every other word in words with item, starting with the first word. Problem 3, PART B Sec 02 Part B (6 pts) def IsWordInPhrases(listPhrases, word): for phrase in listPhrases: if word in phrase: return True return False Q1. It is checking if the word is a substring in any of the phrases Q2. In line 3 it must check to see if the word matches a complete word, so it would need to split the phrase into words so that it could compare each word in the phrase with word. if word in phrase: replace with if word in phrase.split(): Problem 4 Sec 02 Part A (8 pts) def secondDash(word): pos = word.find("-") if pos == -1: return pos pos2 = word[pos+1:].find("-") if pos2 == -1: return -1 return pos2 + pos + 1 ALT Soln: def secondDash(word): dashcount = 0 for index in range(len(word)): if word[index] == '-': dashcount += 1 if dashcount == 2: return index return -1 Part B (8 pts) def transformWord(word): if isVowel(word[0]): pos = word.find("-") pos2 = secondDash(word) if pos == -1: return word word = word[:pos]+"*"+word[pos+1:] if pos2 == -1: return word return word[:pos2]+"*"+word[pos2+1:] # first letter not a vowel last = word[-1] answer = "" for ch in word[:-1]: answer += ch + ch return answer + last Problem 5 Sec 02 Part A (4 pts) def fileToList(filename): answer = [] f = open(filename) for line in f: line = line.strip() bidder = line[:4] line = line[4:] rest = line.split("$") answer.append([bidder, rest[0], rest[1]]) return answer ALT SOLN: def fileToList(filename): answer = [] f = open(filename) for line in f: line = line.strip() line = line[:4] + "$" + line[4:] # add $ as separator answer.append( line.split("$") ) return answer PART B (10 pts) Sec 02 def amountsBidded(data, bidder): answer = [] for item in data: if item[0] == bidder: if item[2] not in answer: answer.append(item[2]) return answer Part C Sec 02 (10 pts) def objectWithHighestBid(data): maxbid = 0 maxitem = "" for item in data: if float(item[2]) > maxbid: maxbid = float(item[2]) maxitem = item[1] return maxitem