HW_SOURCE_FILE = 'hw03.py' ############# # Questions # ############# from operator import add, mul, sub square = lambda x: x * x identity = lambda x: x triple = lambda x: 3 * x increment = lambda x: x + 1 def has_seven(k): """Returns True if at least one of the digits of k is a 7, False otherwise. >>> has_seven(3) False >>> has_seven(7) True >>> has_seven(2734) True >>> has_seven(2634) False >>> has_seven(734) True >>> has_seven(7777) True >>> from construct_check import check >>> check(HW_SOURCE_FILE, 'has_seven', ... ['Assign', 'AugAssign']) True """ "*** YOUR CODE HERE ***" def pingpong(n): """Return the nth element of the ping-pong sequence. >>> pingpong(7) 7 >>> pingpong(8) 6 >>> pingpong(15) 1 >>> pingpong(21) -1 >>> pingpong(22) 0 >>> pingpong(30) 6 >>> pingpong(68) 2 >>> pingpong(69) 1 >>> pingpong(70) 0 >>> pingpong(71) 1 >>> pingpong(72) 0 >>> pingpong(100) 2 >>> from construct_check import check >>> check(HW_SOURCE_FILE, 'pingpong', ['Assign', 'AugAssign']) True """ "*** YOUR CODE HERE ***" def accumulate(combiner, base, n, term): """Return the result of combining the first n terms in a sequence and base. The terms to be combined are term(1), term(2), ..., term(n). combiner is a two-argument, associative function. >>> accumulate(add, 0, 5, identity) # 0 + 1 + 2 + 3 + 4 + 5 15 >>> accumulate(add, 11, 5, identity) # 11 + 1 + 2 + 3 + 4 + 5 26 >>> accumulate(add, 11, 0, identity) # 11 11 >>> accumulate(add, 11, 3, square) # 11 + 1^2 + 2^2 + 3^2 25 >>> accumulate(mul, 2, 3, square) # 2 * 1^2 * 2^2 * 3^2 72 """ total, k = base, 1 while k <= n: total, k = combiner(total, term(k)), k + 1 return total def filtered_accumulate(combiner, base, pred, n, term): """Return the result of combining the terms in a sequence of N terms that satisfy the predicate pred. combiner is a two-argument function. If v1, v2, ..., vk are the values in term(1), term(2), ..., term(N) that satisfy pred, then the result is base combiner v1 combiner v2 ... combiner vk (treating combiner as if it were a binary operator, like +). The implementation uses accumulate. >>> filtered_accumulate(add, 0, lambda x: True, 5, identity) # 0 + 1 + 2 + 3 + 4 + 5 15 >>> filtered_accumulate(add, 11, lambda x: False, 5, identity) # 11 11 >>> filtered_accumulate(add, 0, odd, 5, identity) # 0 + 1 + 3 + 5 9 >>> filtered_accumulate(mul, 1, greater_than_5, 5, square) # 1 * 9 * 16 * 25 3600 >>> # Do not use while/for loops or recursion >>> from construct_check import check >>> check(HW_SOURCE_FILE, 'filtered_accumulate', ... ['While', 'For', 'Recursion']) True """ def combine_if(x, y): "*** YOUR CODE HERE ***" return accumulate(combine_if, base, n, term) def odd(x): return x % 2 == 1 def greater_than_5(x): return x > 5