# Name: ... # CSE 160 # Winter 2022 # Midterm Exam ############################################################################### # Problem 1 ############################################################################### def larger_than_average(input_list): """Finds all numbers in input_list greater than the running average Arguments: input_list (list): list of numbers Returns: A list of all the numbers from input_list that are greater than the average of the numbers earlier than them in input_list """ ret_list = [] sum = 0 counter = 0 for num in input_list: if (counter != 0): if(num > sum/counter): ret_list.append(num) sum = sum + num counter = counter + 1 return ret_list assert larger_than_average([1, 3, 5, 2, 4]) == [3, 5, 4] assert larger_than_average([1]) == [] assert larger_than_average([1, 2, 3, 4, 5]) == [2, 3, 4, 5] ############################################################################### # Problem 2 ############################################################################### def find_content(listings, title, min_rating): """Finds a title with a minimum rating in a grid of listings. Takes a list of lists of movies and their ratings and searches for a given title with a minimum rating. Returns its position if found. Arguments: listings (list): A list of rows of movies. May be empty title (string): The title of movie to search for min_rating (float): Movie must have at least this rating Returns: A list with exactly 2 items. Index 0 represents the row in which 'title' was found. Index 1 represents where in the row it exists. Returns -1 for both if the title with at least min_rating is not found. """ location = [-1, -1] for i in range(len(listings)): row = listings[i] for j in range(len(row)): row_title = row[j][0] rating = row[j][1] if row_title == title and rating >= min_rating: location = [i, j] return location my_listings = [ [["Ozark", 4.2], ["The Witcher", 3.4], ["The Great Baking Show", 5.0]], [["Chef to Table", 4.9], ["Single's Inferno", 2.0], ["Cocomelon", 4]], [["Bee Movie", 5.0], ["Daredevil", 3.6], ["Arcane", 2.0]] ] assert find_content(my_listings, "Daredevil", 3) == [2, 1] assert find_content(my_listings, "Daredevil", 4.5) == [-1, -1] assert find_content(my_listings, "Cocomelon", 1.0) == [1, 2] assert find_content([], "Finding Nemo", 0) == [-1, -1] ############################################################################### # Problem 3 ############################################################################### def calculate_bill(cart, coupon): """Calculate a total order price, applying a coupon and sales tax Arguments: cart (list): list of numbers representing prices of items coupon (number): a float, how much to deduct (pre-tax) off the total Returns: The total price for the given list of items """ amount = 0.0 # Add up item prices for item_price in cart: amount += item_price # Apply coupon amount -= coupon if amount < 0: amount = 0 # Apply 8.7% sales tax amount += amount * 0.087 return amount def assess_purchase(shopping_cart, coupon, budget): """Determines how many of the given items could be purchased, assuming budget Arguments: shopping_cart (list): list of numbers representing prices of items coupon (number): a float, how much to deduct from this order budget (number): a float, the maximum amount of money to spend Returns: The number of items in shopping_cart that can be purchased without exceeding budget. """ num_items = 0 # Calculate the cost for increasing numbers of items # to find the max number that can be purchased within budget for i in range(len(shopping_cart)): total = calculate_bill(shopping_cart[:i + 1], coupon) if total > budget: return num_items num_items += 1 return num_items my_cart = [5.60, 4.75, 3.00] my_coupon = 2.00 my_budget = 12.00 assert round(calculate_bill(my_cart, my_coupon), 5) == 12.33745 assert round(calculate_bill([5.60, 4.75], 2.00), 5) == 9.07645 assert calculate_bill([1.00], 2.00) == 0 assert assess_purchase(my_cart, my_coupon, my_budget) == 2 assert assess_purchase(my_cart, my_coupon, my_budget + 1) == 3 ############################################################################### # Problem 4 ############################################################################### def replace_letter(word, letter, replacement): """Replaces all instances of letter in word with replacement replace_letter should create a copy of word, but in the process replace any instance of letter with replacement. You _cannot_ simply use Python's replace() function. Arguments: word (string): the word to copy and replace letters. Always a string. letter (string): always a string of length 1; the letter to change. replacement (string): always a string of length 1; the new letter. Returns: A new copy of word with all instance of letter changed to replacement """ result = '' for l in word: if l == letter: result += replacement else: result += l return result assert replace_letter('area', 'a', 'o') == 'oreo' assert replace_letter('mississippi', 's', 'z') == 'mizzizzippi' assert replace_letter('nothing', 'z', 'a') == 'nothing' ############################################################################### # Problems N... (aka Part 2) ############################################################################### def part_two(): """States whether or not I have completed part 2 of the midterm. Said part can be found at this URL: https://www.gradescope.com/courses/345007/assignments/1822895 Returns: False until changed to True """ # Change to True once you've completed Part 2 of the Midterm # (If you complete Part 2 but forget to change this, your grade will *not* # be affected) return True