CompSci 101 Sec01 Spring 2015 Solution 1A. Output NOTES on output 1 multiplication occurs first, then the subtraction 7 - 6 is 1 False 7 <= 2 is false 4 7 squared is 49, 49/10 with integer division is 4 2 14 % 6, 6 goes into 14 twice with 2 remaining 1B. s s is in slot 2 of "Was" - note W is in slot 0 shi note pos is 5, so this is city[2:5], which is the characters in slots 2 through 4 ton -3 means the last three characters from the right end of the string Cary the last ciy in the list ['Atlanta', 'Washington'] The cities in slot 1 and 2 2A. def ConvertRupeesToDollars(amount, charge, rate): return amount * rate - charge/100*amount * rate Alternate Solution: def ConvertRupeesToDollars(amount, charge, rate): total = amount * rate fee = charge/100 * total return total - fee 2B. def hotelStay(price, days, tax, fee): # calculate fee based on days if days > 6: fee = 0 elif days > 3: fee = fee/2.0 total = price * days # calculate total price totalPlusTax = total* (1 + tax/100.0) # add tax return totalPlusTax + fee* days # add fee 3A. Q1. It does not work correctly for any of these: remove(data, 'follow') remove(data, 'fruit') Note 'brown' gives the correct answer Q2. It does not add the word to the list if the first letter matches but the second letter does not match, but should add that word. Q3. def removeCorrect(alist, testword): answer = [] for word in alist: if word[0] == testword[0]: if word[1] == testword[1]: pass else: answer = answer + [word] else: answer = answer + [word] return answer Alternate Soln: def removeCorrect2(alist, testword): answer = [] for word in alist: if word[0] == testword[0] and word[1] == testword[1]: pass else: answer = answer + [word] return answer Alternate Soln: def removeCorrect3(alist, testword): answer = [] for word in alist: if word[0] != testword[0] or word[1] != testword[1]: answer = answer + [word] return answer 3B. B1. people, key B2. names, "er" B3. ['Karler', 'Bether', 'Frederick', 'Saraher', 'Bruceer'] B4. [] B5. Mystery crashes since y[0] does not exist B6. Mystery first looks at every word in the list and if any word does not have key in the word it adds it to the right end of the word. Then it creates a new list of those words in which the key appears starting in one of the first four positions. It returns the first such word. B7. The example given crashes it. Another example would be mystery(names, 'only') 4A. def posUpper(word): for i in range(len(word)): if word[i] == word[i].upper(): return i return -1 4B. def posSecondUpper(word): pos1 = posUpper(word) if pos1 == -1: return -1 pos2 = posUpper(word[pos1+1:]) if pos2 == -1: return -1 else: return pos1 + pos2 +1 4C. def emphasize(word): pos1 = posUpper(word) pos2 = posSecondUpper(word) if pos1 == -1: # no uppercase letters return word.upper() if pos2 == -1: # only one uppercase letter return word[:pos1+1] + "ly" + word[pos1+1:] # at least two uppercase return word[:pos1] + word[pos1] + word[pos1] + word[pos1+1:pos2+1] + "ly" + word[pos2+1:] 5A. def playersFrom(data,school): answer = [] for line in data: alist = line.split("#") if alist[2] == school: answer = answer +[alist[0]] return answer #Alternate solution with append def playersFrom(data,school): answer = [] for line in data: alist = line.split("#") if alist[2] == school: answer.append(alist[0]) return answer 5B. def playersOfTypes(data,year1,year2): answer = [] for line in data: alist = line.split("#") if alist[1] == year1 or alist[1] == year2: answer = answer +[alist[0]+ "-" + alist[2]] return answer #Alternate solution with append def playersOfTypes(data,year1,year2): answer = [] for line in data: alist = line.split("#") if alist[1] == year1 or alist[1] == year2: answer.append(alist[0]+ "-" + alist[2]) return answer