## List Comprehension ## def coords(fn, seq, lower, upper): """ >>> seq = [-4, -2, 0, 1, 3] >>> fn = lambda x: x**2 >>> coords(fn, seq, 1, 9) [[-2, 4], [1, 1], [3, 9]] """ "*** YOUR CODE HERE ***" return ______ ## Linked Lists ## # The linked list implementation is at the bottom of this file. def is_sorted(lst): """Returns True if the linked list is sorted. >>> lst1 = link(1, link(2, link(3, link(4)))) >>> is_sorted(lst1) True >>> lst2 = link(1, link(3, link(2, link(4, link(5))))) >>> is_sorted(lst2) False >>> lst3 = link(3, link(3, link(3))) >>> is_sorted(lst3) True """ "*** YOUR CODE HERE ***" # Linked List definition empty = 'empty' def is_link(s): """s is a linked list if it is empty or a (first, rest) pair.""" return s == empty or (type(s) == list and len(s) == 2 and is_link(s[1])) def link(first, rest=empty): """Construct a linked list from its first element and the rest.""" assert is_link(rest), 'rest must be a linked list.' return [first, rest] def first(s): """Return the first element of a linked list s.""" assert is_link(s), 'first only applies to linked lists.' assert s != empty, 'empty linked list has no first element.' return s[0] def rest(s): """Return the rest of the elements of a linked list s.""" assert is_link(s), 'rest only applies to linked lists.' assert s != empty, 'empty linked list has no rest.' return s[1] def print_link(s): """Print elements of a linked list s. >>> s = link(1, link(2, link(3, empty))) >>> print_link(s) 1 2 3 """ line = '' while s != empty: if line: line += ' ' line += str(first(s)) s = rest(s) print(line)