""" Name: Hannah Cheung CSE 160 Summer 2022 Midterm Exam Solution """ # Problem 1 def pin_check(pin, score): """ Calculates given pin score and compare to given score parameter by taking sum of every digit multiplied by that digit's position Arguments: pin (string): String containing digits that represent pin number May be empty. score (int): Score to check with calculated pin Returns: Boolean value whether or not calculated pin score is equal to the score parameter """ actual = 0 for i in range(1, len(pin) + 1): actual += (int(pin[i - 1]) * i) return actual == score assert pin_check("975", 38) assert pin_check("", 0) assert not pin_check("3290", -1) # Problem 2 def long_songs(songs): """ Calculates and returns song names that have a runtime greater than or equal to the average runtime of a song in the songs list parameter. Arguments: songs (list): A list of lists, where the inner lists consists of a song name and its integer runtime (in seconds) Returns: A list of song names that are greater than or equal to the average runtime of a song in the songs list """ sum_runtime = 0 # Calculate average runtime for i in range(len(songs)): song_runtime = songs[i][1] if song_runtime < 0: return None sum_runtime += song_runtime average_runtime = sum_runtime / len(songs) # Determine which songs are at or above average runtime result = [] for i in range(len(songs)): song_name = songs[i][0] song_runtime = songs[i][1] if song_runtime >= average_runtime: result.append(song_name) return result assert long_songs([["Yet to Come", 193], ["Black Swan", 198], ["Life Goes On", 207]]) == ["Life Goes On"] assert long_songs([["Polaroid Love", 185], ["Lemonade", 190], ["Go!", 210], ["WOLFGANG", 190], ["Back Door", 205]]) == \ ["Go!", "Back Door"] assert long_songs([["Star Blossom", 211], ["Lucid Dream", 211]]) == \ ["Star Blossom", "Lucid Dream"] # Problem 3 def nct_x(members, n): """ Combine n contiguous subgroups of the members list that sums to the greatest number and calculates the remaining number of members that were not combined and placed into NCT X Arguments: members (list): List of integers representing current subgroups of members n (int): Number of continuous subgroups of members to combine in members list Returns: The number of remaining members that were not placed into NCT X """ total_members = 0 for i in members: total_members += i max_count = 0 for i in range(len(members) - n + 1): curr_sum = 0 # Update max sum of n subgroups curr_group = members[i:i+n] for j in curr_group: curr_sum += j if curr_sum > max_count: max_count = curr_sum return total_members - max_count assert nct_x([1, 2], 0) == 3 assert nct_x([0, 1, 2, 3, 4], 2) == 3 assert nct_x([3], 1) == 0 def fonzify(sentence, y_count): """ Convert a sentence into a Fonz sentence by adding "ay" with a y_count number of y's in the beginning of a sentence and before punctuation (comma, period, question mark, or exclamation point) Arguments: sentence (string): Sentence to be fonzified y_count (integer): Number of y's in the "ay" added to sentence Returns: Sentence converted to a Fonz sentence """ y = "y" * y_count ay = "a" + y punctuation = [",", ".", "?", "!"] fonzify_sentence = ay + " " for char in sentence: if char in punctuation: fonzify_sentence += " " + ay fonzify_sentence += char return fonzify_sentence assert fonzify("pass the butter, please.", 3) == \ "ayyy pass the butter ayyy, please ayyy." assert fonzify("fantastic", 10) == "ayyyyyyyyyy fantastic" assert fonzify("", 1) == "ay "