# We reuse the following binary operators from last time def add(x, y): return x + y def mul(x, y): return x * y def lesser(x, y): if x < y: return x else: return y def greater(x, y): if x > y: return x else: return y # Also consider the following higher order functions map reduce from toolz import accumulate # When showing off numpy we used the cumulative sum method import numpy as np X = np.arange(20) X[X>=10].cumsum() # Accumulate is a general version of this data = range(10, 20) list(accumulate(add, data)) X[X>=10].cumprod() list(accumulate(mul, data)) data1 = [1, 2, 3, 4, 5] data2 = [10, 20, 30, 40, 50] map(add, data1, data2) reduce(lesser, [5, 3, 2, 7, 3], 999999999) list(accumulate(lesser, [5, 3, 2, 7, 3])) map(lesser, [5, 3, 2, 7, 3], [1, 9, 2, 6, 6]) # Pairwise lesser across the two lists def longer(a, b): ... assert longer('cat', 'mouse') == 'mouse' # because cat is of length 3 and mouse is of length 5 # Use reduce to find the longest word animals = ['cat', 'mouse', 'lion', 'goose', 'giraffe', 'mule'] # Use accumulate to track the longest word over time. # This should be a list where the first element is 'cat', the last is 'giraffe', # and each intermediate element is the longest word up to that point # Use map to create a list of the pairwise longest among the following two lists animals = ['cat', 'mouse', 'lion', 'goose', 'giraffe', 'mule'] fruits = ['apple', 'orange', 'banana', 'date', 'grape', 'strawberry']