程序代写代做代考 python interpreter CS 61A Midterm 1 Review Fall 2017 September 8, 2017 – cscodehelp代写

CS 61A Midterm 1 Review Fall 2017 September 8, 2017
Instructions
Form a small group. Start on the first problem. Check off with a helper or discuss your solution process with another group once everyone understands how to solve the first problem and then repeat for the second problem . . .
You may not move to the next problem until you check off or discuss with another group and everyone understands why the solution is what it is. You may use any course resources at your disposal: the purpose of this review session is to have everyone learning together as a group.
0.1 >>> print(print(‘Welcome to’), print(‘CS 61A’))
1 Functions
1.1 What would Python Display?
(a) 1 /
(b) >>> … …
(c) >>>
(d) >>>
0
def boom():
return 1 / 0
boom
boom()
1.2 What would Python display? (a) 3 + 4
(b) ‘3’ + 4 (c) ‘3 + 4’
(d) ‘3’ + ‘4’

2 Midterm 1 Review
2 Higher-Order Functions
2.1 What would Python display?
(a) (lambda x: x(x))(lambda y: 4)
(b) (lambda x, y: y(x))(mul, lambda a: a(3, 5))
2.2 Write a higher-order function that passes the following doctests.
Challenge: Write the function body in one line.
def mystery(f, x): “””
>>> from operator import add, mul
>>> a = mystery(add, 3)
>>> a(4) # add(3, 4)
7
>>> a(12)
15
>>> b = mystery(mul, 5)
>>> b(7) # mul(5, 7)
35
>>> b(1)
5
>>> c = mystery(lambda x, y: x * x + y, 4)
>>> c(5)
21
>>> c(7)
23
“””
2.3 What would Python display?
>>> foo = mystery(lambda a, b: a(b), lambda c: 5 + square(c)) >>> foo(-2)

2.4 Implement make_alternator.
def make_alternator(f, g): “””
>>> a = make_alternator(lambda x: x * x, lambda x: x + 4)
>>> a(5)
1
6
9
8 25 “””
2.5 Fill in the blanks (without using any numbers in the first blank) such that the entire expression evaluates to 9.
(lambda x: lambda y: ___________________)(_______)(lambda z: z*z)()
2.6 Draw the environment diagram that results from running the code.
def dream1(f):
kick = lambda x: mind() def dream2(secret):
mind = f(secret)
kick(2) return dream2
inception = lambda secret: lambda: secret real = dream1(inception)(42)
Midterm 1 Review 3

4 Midterm 1 Review 3 Recursion
3.1 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.
3.2 Here is a Python function that computes the nth Fibonnacci number. What’s the domain and range of this function? Identify the three parts of this recursive program.
def fib(n):
if n == 0:
return 0 elif n == 1: return 1
else:
return fib(n – 1) + fib(n – 2)
3.3 Implement replace, which takes in a number n, a digit old and a digit new, and returns a number identical to n, but where every occurrence of the digit old is replaced with the digit new.
def replace(n, old, new):
if ________________________________________________:
return 0
last = ____________________________________________ rest = ____________________________________________ if last == old:
return ________________________________________________________________ else:
return ________________________________________________________________

Tree Recursion
3.4 Implement stairs(n), which takes in a number n and returns the number of ways to take n steps given that at each step you can choose to take 1, 2, or 3 steps.
def stairs(n): “””
>>> stairs(5)
13
>>> stairs(10)
274
“””
3.5 Implement stairs(n, k), which takes in a number n and a number k and returns the number of ways to take n steps given that at each step you can choose to take any of 1,2,…,k−2,k−1, or k steps.
def stairs(n, k): “””
>>> stairs(5, 2)
8
>>> stairs(5, 5)
16
>>> stairs(10, 5)
464
“””
Midterm 1 Review 5

6 Midterm 1 Review
4 Exam Preparation Extra Practice
4.1 For each of the expressions in the table below, write the output displayed by the interactive Python interpreter when the expression is evaluated. The output may have multiple lines. If an error occurs, write “Error”, but include all output displayed before the error. If a function value is displayed, write “Function”.
Assume that you have started python3 and executed the following state- ments:
def pup(bark): woof = 10
def yip(yap):
if bark % yap == 0:
return woof * 3 return yap + woof
return yip
def spot(dog): per = 39
if dog > 5: print(“pup”)
if dog > 10:
return pup(per)
def cloud(grr): print(grr * 3)
woof = 9
py = woof // 3
>>> pet = spot(13)
>>> print(cloud(woof + 6))
>>> pet(py)
>>> pet(woof)
>>> pup(py)
>>> pet(3)

4.2 Add parentheses and single-digit integers in the blanks below so that the ex- pression on the second line evaluates to 2017. You may only add paren- theses and single-digit integers. You may leave some blanks empty.
lamb = lambda lamb: lambda: lamb + lamb
lamb(1000)______ + (lambda b, c: b______ * b______ + c______)(lamb(______), 1)______
4.3 Implement the memory function, which takes a number x and a single- argument function f. It returns a function with a peculiar behavior that you must discover from the doctests. You may only use names and call expressions in your solution. You may not write numbers or use features of Python not yet covered in the course.
square = lambda x: x * x double = lambda x: 2 * x
def memory(x, f):
“””Return a higher-order function that prints its memories.
>>> f = memory(3, lambda x: x)
>>> f = f(square)
3
>>> f = f(double)
9
>>> f = f(print) 6
>>> f = f(square) 3
None
“””
def g(h):
print(______________________________________________________________)
return _____________________________________________________________ return g
Midterm 1 Review 7

8 Midterm 1 Review
4.4 Implement a counter that returns a function which accepts digits in a given base and returns the value in base 10 after encountering ‘done’. Numbers that are not digits in the given base are ignored.
Hint: What should parse return? 
􏰗􏰕􏰓􏰑 􏰒 􏰔 􏰖 􏰘
 (1)·2 +0 ·2 +1 ·2+1
Instructor’s Hint: While this problem might seem like something you’ve never learned before, remember to rely on your intuition and experience with the problem-solving process. Revisit the doctests when you feel stuck, run through the code in your head, and ask yourself questions to make progress.
def counter(base):
“””Return a function which accepts digits in a given base andreturns the value in base 10 after encountering ‘done’. Numbers that are not digits in the given base are ignored.
>>> binary = counter(2)
>>> binary(‘done’)
0
>>> binary(1)(0)(1)(1)(‘done’)
11
>>> binary(1)(2)(3)(0)(1)(‘done’)
5
>>> quaternary = counter(4)
>>> quaternary(1)(2)(3)(0)(1)(‘done’) # 1*(4**4) + 2*(4**3) + 3*(4**2) + 0*(4**1) + 1*1
433
“””
def parse(digit, total):
if _________________________________________________:
return _________________________________________________________________________
elif _______________________________________________:
return _________________________________________________________________________
return _____________________________________________________________________________ return _________________________________________________
# see example above
# 2 and 3 are not digits in base 2

Leave a Reply

Your email address will not be published. Required fields are marked *