CompSci 101 Fall 2014 - Exam 1 Makeup Problem 1A: 4.2 6 0 3 Problem 1B: e cer 1 black ['blue', 'red'] Problem 2A: def temperature(tempF): tempC = (tempF - 32) * (5/9.0) if tempF < 100: return str(tempF)+"F" else: return str(tempC)+"C" Problem 2B: def convertPrice(price, day, month): if (month != 'April'): price = price + price * .10 if day=="Saturday" or day =="Sunday": price = price - 10.00 if price < 0: price = -1.0 * price return price ALTERNATE SOLUTION def convertPrice(price, day, month): if (month != 'April'): price = price * 1.10 if day=="Saturday" or day =="Sunday": price = price - 10.00 return abs(price) Problem3A Q1. ["red", "red", "red"], "red" Q2. It is in an infinite loop if there is any color in the list that does not match the color of the second parameter. In that case pos is not updated, so the while condition will not be false to stop the loop. Q3. def countCarsWithColor(clist, color): count = 0 pos = 0 while (pos < len(clist)): if (clist[pos] == color): count += 1 pos += 1 return count Problem 3B: B1. ['Sue', 'Aaron', 'Adam'] B2. ['Aaron', 'Adam'] B3. Adam B4. Mystery returns the first such name that comes after a name that starts with the same letter and is of length greater than 3. B5. ["Mo", "Jo"] - this crashes on line 11. The list x is empty and the list y is empty. The second for loop is ignored, and return [-1] is a position that does not exist. Problem 4: def swap(word,let1,let2): pos1 = word.find(let1) pos2 = word.find(let2) if pos1 < 0 or pos2 < 0: return word word = word[:pos1] + let2 + word[pos1+1:] word = word[:pos2] + let1 + word[pos2+1:] return word Problem 5A: def lastNamesInSect(data, sect, year): nameList = [ ] for line in data: items = line.split(":") if items[-1] == year and items[2]== sect: nameList += [items[1] ] return nameList Problem 5B: def lastNamesWithLetter(data, sect, year, let): alist = lastNamesInSect(data, sect, year) count = 0 for name in alist: if name[0] == let: count += 1 return count