# Name: Sample Solution # CSE 160 # Autumn 2020 # Midterm Exam # Problem 1 def check_sort(nums, first, second): ''' nums - a list of integers first, second - valid indexes into nums Assume first =< second Returns True if numbers in nums between position first (inclusive) and second (exclusive) are sorted in ascending order. Return True if nums is empty or if first and second are equal. Return False otherwise. ''' if nums == [] or first == second: return True for i in range(first, second - 1): if nums[i] > nums[i + 1]: return False return True assert check_sort([10, 12, 13, 14, 5], 1, 4) is True assert check_sort([10000, 2, 1, 2, 3, 10000], 0, 5) is False assert check_sort([], 0, 0) is True # Problem 2 def class_standing(lst): ''' lst - a list of lists of integers representing credit hours The function will sum up the integers in all lists contained in lst. Returns a string: “freshman” if the student has less than 45 credits total, “sophomore” if they have at least 45 credits and less than 90 credits, "junior" if they have at least 90 credits and less than 135 credits, and “senior” if they have 135 credits or more ''' total_credits = 0 for quarter in lst: for course_credits in quarter: total_credits += course_credits if total_credits < 45: return "freshman" elif total_credits < 90: return "sophomore" elif total_credits < 135: return "junior" else: return "senior" assert class_standing([[]]) == "freshman" assert class_standing([[5, 5], [3, 1, 2], [10]]) == "freshman" assert class_standing([[5, 5, 5], [4, 5, 5, 1], [5, 5, 5, 1], [4, 4, 3, 1], [5, 5, 4], [3, 2, 5, 3]]) == "sophomore" # Problem 3 def verify_lengths(words, min, max): ''' words - a list of strings min, max - positive integers where min <= max Returns True if all strings in words have a length between min and max (both inclusive). Return False otherwise. Return False if words is an empty list. ''' if words == []: return False for word in words: if (len(word) > max) or (len(word) < min): return False return True assert verify_lengths([], 2, 3) is False assert verify_lengths(["cats", "are", "cool"], 3, 4) is True assert verify_lengths(["12345", "12345", "12", "1"], 2, 5) is False # Problem 4 def weave_max(list1, list2): ''' list1, list2 - non-empty lists of numbers Returns a new list that contains the larger of the first numbers in list1 and list2, followed by the larger of the second numbers in list1 and list2, etc. until one of the two lists runs out. After one of the lists runs out, the remaining numbers from the longer list should be appended to the new list. ''' len1 = len(list1) len2 = len(list2) new_list = [] # list2 is shorter if len1 > len2: for i in range(len2): if list1[i] > list2[i]: new_list.append(list1[i]) else: new_list.append(list2[i]) # append rest of list1 new_list.extend(list1[len2:]) else: # list1 is shorter for i in range(len1): if list1[i] > list2[i]: new_list.append(list1[i]) else: new_list.append(list2[i]) # append rest of list2 new_list.extend(list2[len1:]) return new_list assert weave_max([1, 54, 6, 8], [2, 3, 7, 11, 9, 1]) == [2, 54, 7, 11, 9, 1] assert weave_max([1], [2]) == [2] # Problem 5 def star_changer(word): ''' word - a string Returns a new string with stars (*) substituted for every other character, starting with the second character. The number of stars in the new string should match the index of the character you are replacing. ''' new_string = "" for i in range(len(word)): if i % 2 == 0: new_string = new_string + word[i] else: for j in range(i): new_string = new_string + "*" return new_string assert star_changer("star") == "s*a***" assert star_changer("one") == "o*e" assert star_changer("") == "" assert star_changer("a") == "a" assert star_changer(" ") == " *" assert star_changer("long word") == "l*n*** *****o*******d" # 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.