# Name: ... # CSE 160 # Autumn 2021 # Midterm Exam # Problem 1 def check_same_parity(nums_lst): ''' Takes in a list of lists of integers and returns True if every number in each sublist has the same parity. Otherwise, the function should return False. Assume that nums_lst is not empty and each sublist contains at least one integer. Arguments: nums_lst: a list of lists of integers. Returns: a boolean value indicating if all numbers in each sublist shares the same parity ''' # Guaranteed to have at least one sublist for lst in nums_lst: # Guaranteed to have at least one element in sublist is_odd = (lst[0] % 2) == 1 for i in lst[1:]: # looking for an odd, found an even if is_odd and (i % 2 == 0): return False # looking for an even, found an odd elif not is_odd and (i % 2 == 1): return False return True assert check_same_parity([[3, 9, 7], [2, 4], [1]]) is True assert check_same_parity([[2, 4], [3, 4, 5]]) is False # Problem 2 def max_nucleotide_freq(frequency_list): ''' Takes a list of lists containing a unique nucleotide and its frequency in a dataset. Returns a string describing the valid nucleotide (A, C, T, G) with the highest frequency and its associated frequency. Assume there is at least one pair that has both a valid nucleotide base and frequency >= 1 Arguments: frequency_list: a list of lists, where each list has a nucleotide base as a string in the first index, and the frequency of that base as an integer in the second index Returns: "{base} has {max frequency} occurrences!", where {base} represents the nucleotide base with the max frequency Return "No data!" if frequency_list is empty. ''' # your solution code should start here if frequency_list == []: return 'No data!' max_nucleotide = '' max_freq = 0 for pair in frequency_list: if (pair[0] == 'A' or pair[0] == 'C' or pair[0] == 'G' or pair[0] == 'T'): if pair[1] > max_freq: # pick one that occurs first if a tie max_nucleotide = pair[0] max_freq = pair[1] return max_nucleotide + ' has ' + str(max_freq) + ' occurrences!' assert max_nucleotide_freq([['A', 2], ['G', 6], ['C', 20]]) ==\ "C has 20 occurrences!" assert max_nucleotide_freq([['A', 14], ['G', 6]]) ==\ "A has 14 occurrences!" assert max_nucleotide_freq([['A', 1]]) == "A has 1 occurrences!" # Problem 3 def find_activity(weather): ''' Takes in a string that represents a weather condition. Returns the corresponding activity as a string. Arguments: weather: a string representing the current weather Returns: A string representing the appropriate activity to participate in ''' # your solution code should start here if 'sunny' in weather: return 'play tennis' elif 'cloudy' in weather: return 'do homework' elif 'rainy' in weather: return 'make food' else: return 'take a nap' assert find_activity("sunny") == "play tennis" assert find_activity("cloudy") == "do homework" assert find_activity("rainy") == "make food" def create_schedule(forecast): ''' Takes in a forecast that is a list of weather predictions. Returns a list of strings representing an appropriate schedule given the forecast. Arguments: forecast: a non-empty list of strings representing upcoming weather forecasts Returns: A list of strings representing appropriate activities for each weather condition ''' # your solution code should start here schedule = [] for weather in forecast: schedule.append(find_activity(weather)) return schedule assert create_schedule(["cloudy", "still cloudy", "sunny", "drizzly", "rainy"]) == ["do homework", "do homework", "play tennis", "take a nap", "make food"] assert create_schedule(["super sunny", "considerably cloudy"]) ==\ ["play tennis", "do homework"] # Problem 4 def first_n_indices(s, target, n): ''' Returns a list of the first n indices where target appears in the given string s. Returns an empty list if s does not contain the target character or n is 0. Assume target will always be a single character and n will be an int >= 0 Arguments: s: an input string to find the first n indices of target: a target character. This will always be a single character n: an integer limiting how many indices we want to return from the function Returns: A list a list of the first n indices where target appears in the given string s. ''' # your solution code should start here num_found = 0 output_list = [] for idx in range(len(s)): if s[idx] == target and num_found < n: output_list.append(idx) num_found += 1 return output_list assert first_n_indices("abcde", "a", 2) == [0] assert first_n_indices("a-be-c-", "-", 2) == [1, 4] assert first_n_indices("Go ea-ee-ee-ee-ee-ee-ee-sy on me baby~", "g", 2) == [] # Problem 5 def squash_in_half(grid): ''' Takes in a grid that is a list of lists of integers representating a rectangular grid. Returns a new 'squashed' version of the given grid, where every two consecutive rows in the original grid have been combined into a single row in the output grid. Assume grid and its sublists are all non-empty. Arguments: grid: a list of lists representating a rectangular grid. Returns: a list of lists representing a squashed grid ''' # your solution code should start here new_grid = [] for row_idx in range(0, len(grid) - 1, 2): new_row = [] for col_idx in range(len(grid[0])): new_row.append(grid[row_idx][col_idx] + grid[row_idx + 1][col_idx]) new_grid.append(new_row) # add extra odd row if needed if len(grid) % 2 == 1: new_row = [] for col_idx in range(len(grid[0])): new_row.append(grid[len(grid) - 1][col_idx]) new_grid.append(new_row) return new_grid assert squash_in_half([[1, 1], [0, 1], [0, 1], [2, 2]]) == [[1, 2], [2, 3]] assert squash_in_half([[-1, 0], [0, 1], [3, 2]]) == [[-1, 1], [3, 2]] # ANSWER the following questions as COMMENTS # (1 pt) Did you work on this quiz alone or collaborate with others? # If you collaborated with others, list full names and UWNetIDs # of everyone you collaborated with.