Lab 2: Nested Function Call Exercises
Do these exercises on the whiteboard with your partner(s).
Exercise Nested 1
def h(s):
return '##' + s.lower()
def q(s):
return s.upper() + '?'
Answer the following questions for the functions h and q defined above:
-
Nested 1a:: What are the values of each of the following expressions?
h(q('d'))q(h('e'))h(q(h('F')))
-
Nested 1b: Using
h,q, and'F', what is a different expression thanh(q(h('F')))that returns the same value? - Nested 1c: Using
h,q, and'g', write three different expressions that evaluate to the string'##G???'
Exercise Nested 2
def z():
""" zero """
return 0
def d(n):
""" double """
return 2*n
def i(n):
""" double and increment """
return 2*n + 1
Answer the following questions for the functions z, d, and i defined above:
-
Nested 2a: What are the values of each of the following expressions?
i(d(i(z())))d(i(i(z())))
-
Nested 2b: Using only calls to
d,i, andz, write expressions that evaluate to the following integers:- 7
- 10
- 13
- 15
- 16
-
Nested 2c: For any nonnegative integer n, is it possible to express n with an expression involving calls to only
d,i, andz? Explain your answer. - Nested 2d: For any integer n expressible involving calls to only
d,i, andz, can there be more than one expression involving calls to onlyd,i, andzthat evaluates to n?
Table of Contents
- Lab 2 Home
- Part 1: Function Call Exercises
- Part 2: Exercises from the Function Lecture Notebook
- Part 3: Debugging in Thonny