CompSci 101 Exam 1 Sec 01 and Sec 02 Fall 2014 Solutions Note Problems 1 and 5 are different for the two sections. SEC 01 PROBLEM 1. A. output: 5.1 17 2 2 SEC 01 PROBLEM 1. B. output: e Novr -1 dog ['parrot', 'fox'] SEC 02 PROBLEM 1. A. output: 4.1 15 1 1 SEC 02 PROBLEM 1. B. output: g gustt 2 parrot ['dog', 'parrot', 'fox'] PROBLEM 2 A. def equationsEqual(x): equation1 = 2 *((x + 1)**2) - 1 equation2 = (2*x + 1) **2 return equation1 == equation2 ALTERNATIVE SOLN: def equationsEqual(x): equation1 = 2 *((x + 1)*(x+1)) - 1 equation2 = (2*x + 1)* (2*x + 1) return equation1 == equation2 ALTERNATIVE SOLN: def equationsEqual(x): equation1 = 2 *((x + 1)**2) - 1 equation2 = (2*x + 1) **2 if equation1 == equation2: return True else: return False ALTERNATIVE SOLN: def equationsEqual(x): return (2 *((x + 1)**2) - 1) == (2*x + 1) **2 PROBLEM 2. B. def convertPrice(price, day, month): if not(month == 'January' or month == "July"): price = price + price * .10 if day=="Monday": price -= 5.0 if price < 1.0: price = 1.0 return price ALTERNATIVE SOLUTION def convertPrice2(price, day, month): if (month == 'January' or month == "July"): price = price # you have to have at least one statement in the body else: price = price + price * .10 if day=="Monday": price -= 5.0 if price < 1.0: price = 1.0 return price ALTERNATIVE SOLUTION def convertPrice3(price, day, month): if (month == 'January' or month == "July") : if day == "Monday": if price <= 6: return 1.0 else: return price - 5 else: return price else: if day == "Monday": if price + price * .10 <= 6: return 1.0 else: return price + price * .10 - 5.0 else: return price * 1.10 PROBLEM 3. A. Q1. doHaveCarWithColor(carColors,'blue') returns False and should return True Q2. The function returns after executing the for loop just once. The If statement returns a value whether it the condition is True or False. Q3. To correct it, remove the else case. Here is the correct Code: def doHaveCarWithColor(clist, color): for carColor in clist: if carColor == color: return True return False PROBLEM 3 B. B1. ['Sarah', 'Aaron', 'Salman'] B2. ['Sarah', 'Salman'] B3. Sarah B4. Mystery returns the name of length greater than 4 that starts with S. If there are multiple such names, it returns the first one. B5. ["Sue", "Beth"] After executing the first for loop the list x is empty, the second for loop does not execute since there are no items in the list. The program crashes on y[0] as there are no items in the list. PROBLEM 4. def stretchOut(word): word = word.lower() if word[0] in "aeiou" or len(word) <= 2: return word return word[0] + word[0] + word[1] + word[1] + word[2:] + "eh" ALTERNATIVE SOLUTION: def stretchOut(word): word = word.lower() if word[0] in "aeiou": return word if len(word) <=2: return word return word[0] + word[0] + word[1] + word[1] + word[2:] + "eh" ALTERNATIVE SOLUTION: def stretchOut(word): word = word.lower() if IsVowel(word[0]): return word if len(word) <=2: return word newWord = word[0] * 2 + word[1] * 2 newWord += word[2:] + "eh" return newWord ALTERNATIVE SOLUTION: def stretchOut(word): word = word.lower() if len(word) < 3 or IsVowel(word[0]): return word newWord = "" for index in range(len(word)): if index < 2: newWord += word[index] + word[index] else: newWord += word[index] newWord += "eh" return newWord SEC 01 PROBLEM 5A. def listOfSectNums(alist, year): secList = [ ] for line in alist: items = line.split(":") if items[-1] == year: secList += [items[2] ] return secList ALTERNATIVE SOLUTION WITH append def listOfSectNums(alist, year): secList = [ ] for line in alist: items = line.split(":") if items[-1] == year: secList.append(items[2]) return secList SEC 01 PROBLEM 5B. def numberOf(data, sect, year): somelist = listOfSectNums(data, year) count = 0 for item in somelist: if item == sect: count += 1 return count ALTERNATIVE SOLUTION: def numberOf(data, sect, year): somelist = listOfSectNums(data, year) sectList = [] for item in somelist: if item == sect: sectList += [item] return len(sectList) ALTERNATIVE SOLUTION: def numberOf(data, sect, year): return listOfSectNums(data, year).count(sect) SEC 02 PROBLEM 5A. def listOfYears(data, sect): yearList = [ ] for line in data: items = line.split(":") if items[2] == sect: yearList += [items[-1] ] return yearList ALTERNATIVE SOLUTION with APPEND: def listOfYears(data, sect): yearList = [ ] for line in data: items = line.split(":") if items[2] == sect: yearList.append(items[-1]) return yearList SEC 02 PROBLEM 5B. def numberOf(data, sect, year): somelist = listOfYears(data, sect) count = 0 for item in somelist: if item == year: count += 1 return count ALTERNATIVE SOLUTION: def numberOf(data, sect, year): somelist = listOfYears(data, sect) yearList = [] for item in somelist: if item == year: yearList += [item] return len(yearList) ALTERNATIVE SOLUTION: def numberOf(data, sect, year): return listOfYears(data, sect).count(year)