Higher Order Functions
Question1
Write a make_skipper
, which takes in a number n and outputs a function. When this function takes in a number x, it prints out all the numbers between 0 and x, skipping every nth number(meaning skip any value that is a multiple of n).
1 | # My solution |
1 | # PDF's solution which uses range(range(start, stop[, step])) |
EXTRA:Question 2
Write make_alternator which takes in two functions, f and g, and outputs a function. When this function takes in a number x, it prints out all the numbers between 1 and x, applying the function f to every odd-indexed number and g to every even-indexed number before printing.
1 | def make_alternator(f, g): |
Recursion
Question 0
a) What are three things you find in every recursive function?
1.One or more Base Cases
2.Ways to make the problem into a smaller problem of the same type(so that it can be solved recursively).
3.One or more Recursive Cases that solve the smaller problem and then uses the solution the smaller problem to solve the original(large) problem
b) When you write a Recursive function, you seem to call it before it has been fully defined. Why doesn’t this break the Python interpreter? Explain in haiku if possible.
When you define a function, Python does not evaluate the body of the function.
Python does not care
about a function’s body
until it is called
Question 1
Question 1
Hint: Domain is the type of data that a function takes in as argument. The Range is the type of data that a function returns.
E.g. the domain of the function square is numbers. The range is numbers.
a) Here is a Python function that computes the nth Fibonnaci number. What’s it’s domain and range? Identify those three things from Q0a
1 | def fib(n): # Domain is integers, Range is integers |
Write out the recursive calls made when we do fib(4) (this will look like an upside down tree).
b) What does the following cascade2 do?
1 | def cascade2(n): |
Takes in a number n and prints out n, n excluding the ones digit, n excluding the tens digit, n excluding the hundreds digit, etc, then back up to the full number
c) Describe what does each of the following functions mystery and fooply do. Identify the three things from Q0a:
1 | def mystery(n): |
Question 2
Mario needs to jump over a series of Piranha plants, represented as an integer composed of 0’s and 1’s. Mario only moves forward and can either step (move forward one space) or jump (move forward two spaces) from each position. How many different ways can Mario traverse a level without stepping or jumping into a Piranha plant? Assume that every level begins with a 1 (where Mario starts) and ends with a 1 (where Mario must end up).
The problem seems to be unclear, I don’t know how to understand this.The solution just copies from the PDF
1 | def mario_number(level): |
EXTRA Challage: Question 3
Implement the combine function, which takes a non-negative integer n, a two-argument function f, and a number result. It applies f to the first digit of n and the result of combining the rest of the digits of n by repeatedly applying f (see the doctests). If n has no digits (because it is zero), combine returns result. Assume n is non negative.
1 | from operator import add, mul |
The iteration code of this problem may be like this
1 | from operator import add, mul |
So we can simply transform the code from iteration to recursion, only need to keep the necessary state.