""" Lab 04 Optional Questions """ from lab04 import * # Q5 def map(fn, lst): """Maps fn onto lst using mutation. >>> original_list = [5, -1, 2, 0] >>> map(lambda x: x * x, original_list) >>> original_list [25, 1, 4, 0] """ "*** YOUR CODE HERE ***" # Q6 def pascal(row, column): """Returns a number corresponding to the value at that location in Pascal's Triangle. >>> pascal(0, 0) 1 >>> pascal(0, 5) # Empty entry; outside of Pascal's Triangle 0 >>> pascal(3, 2) # Row 4 (1 3 3 1), 3rd entry 3 """ "*** YOUR CODE HERE ***" # Q7 def insert_into_all(item, nested_list): """Assuming that nested_list is a list of lists, return a new list consisting of all the lists in nested_list, but with item added to the front of each. >>> nl = [[], [1, 2], [3]] >>> insert_into_all(0, nl) [[0], [0, 1, 2], [0, 3]] """ "*** YOUR CODE HERE ***" def subseqs(s): """Assuming that S is a list, return a nested list of all subsequences of S (a list of lists). The subsequences can appear in any order. >>> seqs = subseqs([1, 2, 3]) >>> sorted(seqs) [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]] >>> subseqs([]) [[]] """ "*** YOUR CODE HERE ***" # Q8 def merge(lst1, lst2): """Merges two sorted lists. >>> merge([1, 3, 5], [2, 4, 6]) [1, 2, 3, 4, 5, 6] >>> merge([], [2, 4, 6]) [2, 4, 6] >>> merge([1, 2, 3], []) [1, 2, 3] >>> merge([5, 7], [2, 4, 6]) [2, 4, 5, 6, 7] """ "*** YOUR CODE HERE ***" # Q9 def mergesort(seq): """Mergesort algorithm. >>> mergesort([4, 2, 5, 2, 1]) [1, 2, 2, 4, 5] >>> mergesort([]) # sorting an empty list [] >>> mergesort([1]) # sorting a one-element list [1] """ "*** YOUR CODE HERE ***"