CompSci 101 Sec02, Fall 2025 Exam 1 Problem 1 a truck <- starts with truck b ti c ropeboatropeboat d 5 e windya f False g beaul h ['boat', 'sail', 'windy'] i ['run', 'five', 'miles'] j iful k cool8hot8windy m 6.2 n 19.5 o 8 Problem 2 PARTS A-C [9, 5, 2, 8] [7, 2, [9, 1]] # -2 if have [7, 2, 9, 1] [3, 5, 3, 5] # -2 if have [3,5] [3,5] OR [[3,5],[3,5]] PART D [6, 7, 2, 3] [6, 7, 2, 3] PART E [6, 5, [4], 3, 9] [6, 5, [4], 3] Problem 3 PART A result = lst[0][3] + lst[-1][-1] OR result = lst[-3][-3] + lst[2][2] OR ANY COMBINATION OF THESE SUCH AS: result = lst[0][-3] + lst[-1][2] PART B result = word[4:7] + word[1:3] OR result = word[-6:-3] + word[-9:-7] OR ANY COMBINATION OF THESE SUCH AS: result = word[4:-3] + word[-9:3] PART C result = lst[2][0][3:] OR result = lst[-1][-1][-4:] OR ANY COMBINATION OF THESE SUCH AS: result = lst[2][-1][3:] PART D word = "plingstrainstraystrop" temp = word.split("str") result = "pl".join(temp) OR result = "pl".join(word.split("str")) Problem 4 def purchaseBooks(cost, taxPercent, discount): result = cost - (cost * discount/100.0) result = result + (result * taxPercent/100.00) return result OR def purchaseBooks(cost, taxPercent, discount): result = cost - (cost * (discount * 0.01)) result = result + (result * (taxPercent * 0.01)) return result OR def purchaseBooks(cost, taxPercent, discount): result = cost * (1 - discount/100.0) result = result *(1 + taxPercent/100.00) return result OR def purchaseBooks2(cost, taxPercent, discount): num1 = discount/100 # works whether 100 is int of float num2 = taxPercent/100 # works whether 100 is int of float result = cost - num1*cost answer = result + result*num2 return answer Problem 5 def giveAwayFruit(amount1, amount2, limit): answer = "" if amount1 > limit: apple = amount1 - limit + 2 answer = str(apple) + " A; " else: answer = "0 A; " if amount2 > limit: banana = amount2 - limit + 4 answer += str(banana) + " B" else: answer += "0 B" return answer OR def giveAwayFruit(amount1, amount2, limit): if amount1 > limit: apple = amount1 - limit + 2 else: apple = 0 if amount2 > limit: banana = amount2 - limit + 4 else: banana = 0 answer = str(apple) + " A; " + str(banana) + " B" return answer Problem 6 def compose(wordA, wordB, wordC, numA, numB): num = random.randint(1,3) ans = "" if num == 1: ans = wordA elif num == 2: ans = wordB else: ans = wordC num = random.randint(numA, numB) return ans + str(num) OR def compose(wordA, wordB, wordC, numA, numB): word = random.choice([wordA, wordB, wordC]) num = random.randint(numA, numB) return word + str(num) Problem 7 PART A: 4 5 8 PART B: 4 5 9 PART C: 7 5 9 PART D: 4 8 7 PART E: The code does not check all the cases. For example, if num1 is the largest, it doesn't check how num2 and num3 compare. OR Three values could be presented six different ways: a b c, a c b, b a c, b c a, c a b, c b a. This function only checks and prints them four ways. PART F: Semantic PART G: In line 4 we need another if statement to compare num2 vs num3. In line 10 we need another if statement to compare num2 vs num3. OR In both if statements, we need to add another if statement to compare num2 vs num 3. YOU WERE NOT SUPPOSED TO WRITE THE CODE BUT HERE IS WHAT IT WOULD LOOK LIKE: def sortNumsCorrect(num1, num2, num3): if num1 > num2: if num1 > num3: if num2 > num3: print(num3, num2, num1) else: print(num2, num3, num1) else: print(num2, num1, num3) else: if num1 > num3: print(num3, num1, num2) else: if num2 > num3: print(num1, num3, num2) else: print(num1, num2, num3)