Published using Google Docs
160 Midterm (21au)
Updated automatically every 5 minutes

CSE 160 Midterm Problems (21au)

Problem 1:

Write a function check_same_parity(nums_lst) where nums_lst is a list of lists of integers. The function should return True if for EVERY sublist, every number in the list has the same parity (they are all even or all odd). Otherwise, the function should return False . Assume that nums_lst is not empty and that each sublist contains at least one number. For example:

check_same_parity([[3, 9, 7], [2, 4], [1]]) should return True because in each sublist, the numbers all have the same parity (the first sublist has all odds, the second sublist has all evens, and the third sublist has all odds). On the other hand:

check_same_parity([[2, 4], [3, 4, 5]]) should return False because in the second sublist, the numbers do not all have the same parity (there is a mix of even and odd numbers).

Problem 2:

Write a function max_nucleotide_freq(frequency_list), where frequency_list is a list of nucleotide/frequency pairs. Each pair is represented by a list of the form: [base, frequency], where base is a string representing a nucleotide base and frequency is a positive integer that represents the base’s corresponding frequency in a dataset. For example, the pair ['G', 6] means that there are 6 occurrences of the nucleotide guanine in the dataset. An example frequency_list could look like this: [['A', 2], ['G', 6], ['C', 20]]. Valid nucleotide bases are 'A', 'C', 'G', and 'T' and they are case-sensitive, meaning 'a', 'c', 'g', and 't' are not valid bases. However, there could be invalid bases in frequency_list that are not 'A', 'C', 'G', or 'T'. A particular base will only occur at most once in frequency_list.

Find the valid nucleotide base that has the highest frequency in frequency_list. Return the string ''base has max frequency occurrences!'', where ''base'' represents the valid nucleotide base with the maximum frequency. For example, if A has the maximum frequency and has 14 occurrences, then you would return: 'A has 14 occurrences!' The output should follow the same format for maximum frequency of 1 as well, meaning that if A still has the maximum frequency and has 1 occurrence instead, you should return ‘A has 1 occurrences!’

We can assume that if frequency_list is not empty, there is at least one pair that has both a valid nucleotide base and a frequency >= 1. If multiple pairs have the same frequency, then pick the pair that occurs first in frequency_list.

Return 'No data!' if frequency_list is empty.

Note: You may not use the function max for this problem (or any other problem in the Midterm).

Problem 3:

Write a function find_activity(weather) which takes in a string representing the current weather and returns a string indicating an appropriate activity using these criteria:

Your function should accept variations within the three weather categories as long as the key word (case-sensitive) is included. For example, if the input is “slightly sunny”, “ssunny”, or “non-sunny” , you should return “play tennis”. You can assume that two or more key weather words will not appear in the same string. You may find the in operation on strings useful.

Then, write a function  create_schedule(forecast) which takes in a list of weather predictions and returns a list of appropriate activities for those weather conditions. The returned list should have activities listed in the same index as the associated weather in the forecast. For example, if “cloudy” is at index 1 in forecast, “do homework” should be in index 1 in the returned list. Your implementation of create_schedule should call the function find_activity. Assume that the given forecast list contains at least one element. You should not modify the input list.

For example, if
tomorrow = ["cloudy", "still cloudy", "sunny", "drizzly", "rainy"] 
then a call to
create_schedule(tomorrow)should return:
["do homework", "do homework", "play tennis", "take a nap", "make food"]

Problem 4:

Question: Write a function first_n_indices(s, target, n) which returns a list of the first n indices where the character target appears in the string s. The list of indices should be sorted by indices in increasing order. If target appears fewer than n times, just return a list of all the indices where it occurs. Assume n is a non-negative integer.

Return an empty list if s does not contain the target character or n is 0.

first_n_indices("abcde", "a", 2) should return [0], since the character “a” is at index 0.

first_n_indices("a-be-c-", "-", 2) should return [1, 4], since the character “-” is at index 1, 4, 6, but we only get the first 2 indices.

Problem 5:

Write a function squash_in_half(grid) which takes in a list of lists of integers representing a rectangular grid and returns a new list of lists where every second row in the input grid has been combined with the previous row into a single row in the output grid. So the first row should be combined with the second row, the third row with the fourth, etc. Rows are combined by adding the values down the columns. If the input grid has an odd number of rows, the last row should just be included in the output grid unmodified (as if we had appended an extra row of 0s to the end of the given grid).

For example, squash_in_half([[1, 1], [0, 1], [0, 1], [2, 2]]) would return [[1, 2], [2, 3]], and

squash_in_half([[-1, 0], [0, 1], [3, 2]]) would return [[-1, 1],[3, 2]]

You can assume that the given grid is a valid representation of a rectangular grid (meaning all of the rows are the same length). You can assume none of the rows are empty, and that grid itself is not an empty list. You should not modify the input grid.

------------------------------------------------------------------------------------------------------------------------

At the end of the file, BE SURE TO ANSWER THE QUESTION ABOUT who you worked with on the exam (even if you worked alone!). You will need the full name and UWNetID (e.g. rea2000, NOT student ID number!) of each person in your group. Max group size = 4.

------------------- End of Midterm! -------------------