7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
1/43
TA ReviewSessionStephen Martinis, Keegan Mann, Albert Wu,Julia Oh
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
2/43
Overview
Evaluation Higher Order Functions Newton's Method
Environment Diagrams Questions
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
3/43
Evaluation
Process of taking an expression and executingthe commands it details according to howPython is defined as a language.
How you do anything in a program.
Here is a review of how to evaluate things.
Not exhaustive
Doesn't deal with environment diagrams
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
4/43
Evaluation Rules: Names
To find a name (variable)
1. Look in localframe first
2. Look up the name in the parent frame
recursively.
3. If there is no parent frame, raise an Error
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
5/43
Evaluation Rules: Assignment
To assign a value to a variable
x = add(2, 3)
1. Evaluate the right side of = sign.
2. Bind name on left side of = sign to value from
step 1
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
6/43
Evaluation Rules: Functions
1. Evaluate the operator
2. Evaluate the operands
3. Apply the operator to the operands
foo(7 * 2, lambda y: x(y), add(3, mul(8,4)))
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
7/43
Evaluation Rules: Definitions
To define function:def foo():
return 3
1. Create a new function
2. Label its parent as the current frame
3. Bind the function to its name in the current
frame
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
8/43
Evaluation (questions)
>>> def foo(x):
def bar(x):
return x + y
y = 6
return bar
>>> y = 5
>>> foo(3)(5)
______
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
9/43
Evaluation (questions) SOLN
>>> def foo(x):
def bar(x):
return x + y
y = 6
return bar
>>> y = 5
>>> foo(3)(5)
11
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
10/43
Evaluation (questions)
>>> def doctor(who):
if knocks == 4:
return 11return 10
>>> def ood(angry):
if angry > 0:return red
return blue
>>> knocks = 4
>>> red = 2
>>> blue = 5>>> who = 0
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
11/43
Evaluation (questions)
Eval to Outputs
doctor(15)
doctor(36) == 10
print(print(red + blue))
ood(doctor(42))
knocks -= ood(who)
print(doctor(who))
ood(knocks)
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
12/43
Evaluation (questions) SOLN
Eval to Outputs
doctor(15) 11 11
doctor(36) == 10 False False
print(print(red + blue)) None 7
None
ood(doctor(42)) 2 2
knocks -= ood(who) N/A Nothing
print(doctor(who)) None 10
ood(knocks) 5 5
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
13/43
Functions
Syntax
def (, , ...):
Ex:
def foo(bar, baz): return 3
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
14/43
Functions (questions)
Q1
Define a function called denerowhich takes
two numbers and finds their difference.
Q2
Define a function harveywhich takes noarguments and returns the number 61.
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
15/43
Functions (questions) SOLN
Q1
def denero(a, b):
return a - b
Q2
def harvey():return 61
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
16/43
Higher-Order Functions
What are the inputs
and outputs?
numbers
booleans
strings
functions
def double(f):
def h(x):
return f(f(x))
return h
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
17/43
Higher-Order Functions (questions)
def test(x):
def review(f):
return f(x)
return review
def half(x):print(x // 2)
Eval to Outputs
half(3) None 1
test(5)
test(5)(half)
test(5)(test)
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
18/43
Higher-Order Functions (questions)SOLN
def test(x):
def review(f):
return f(x)
return review
def half(x):print(x // 2)
Eval to Outputs
half(3) None 1
test(5) FUNC FUNC
test(5)(half) None 2
test(5)(test) FUNC FUNC
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
19/43
Higher-Order Functions (questions)
def silly(name):
def fun():
print('first')
return name(y)
print('second')
return fun
def bop(y):
return y // 10 + 1
y = 34
reserve = silly(bop)
Eval to Outputs
reserve()
silly(bop(y))
silly(print)()
bop(reserve())silly(reserve)()
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
20/43
Higher-Order Functions (questions)SOLN
def silly(name):
def fun():
print('first')
return name(y)
print('second')
return fun
def bop(y):
return y // 10 + 1
y = 34
reserve = silly(bop)
Eval to Outputs
reserve() 4 first
4
silly(bop(y)) func secondfunc
silly(print)() None second
first
34
bop(reserve()) 1 first1
silly(reserve)() ERROR second
firstERROR
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
21/43
Higher-Order Functions (questions)
def one(x):
def two():
def two(y):
def three(z):
return x + y + z
return x(y) + z
return three(y)
return three(z)
return threey = 4
return two(x)
return two
>>> one(lambda x: x*x)
>>> one(lambda x: x*x)()
20>>> one(lambda x: x*x)()(4)
TypeError ...
Cross out lines on the right sothat the doctests below pass.
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
22/43
Higher-Order Functions (questions)SOLN
def one(x):
def two():
def two(y):
def three(z):
return x + y + z
return x(y) + z
return three(y)
return three(z)
return threey = 4
return two(x)
return two
>>> one(lambda x: x*x)
>>> one(lambda x: x*x)()
20>>> one(lambda x: x*x)()(4)
TypeError ...
Cross out lines on the right sothat the doctests below pass.
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
23/43
Lambda Expressions
To evaluate a lambda
lambda x: 2 * x
Create a new function
Label its parent as the current frame
Do NOT evaluate the expression in the colon
yet
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
24/43
Lambda Expressions
def (, ...):
return
def hi():
return 1
def square(x):
return x * x
def mul(a, b):
return a * b
= lambda :
hi = lambda : 1
square = lambda x: x * x
mul = lambda a, b: a * b
What are the intrinsic names of lambdas?
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
25/43
Lambda Expressions (questions)
Q1
def one(x):def two(y):
return x + y
return two
Q2
def branch(cond):if cond:
return one(1)
return one(0)
Convert into lambda expressions.
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
26/43
Lambda Expressions (questions)SOLN
Q1
def one(x):def two(y):
return x + y
return two
Q2
def branch(cond):if cond:
return one(1)
return one(0)
Convert into lambda expressions.
Q1one = lambda x: lambda y: x + y
Q2branch = lambda cond: one(1) if cond else one(0)
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
27/43
Lambda Expressions (questions)
mul = lambda a, b: a * b
curry = lambda f: lambda x: lambda y: f(x, y)
new_mul = curry(mul)(3)
Eval to Outputs
curry(mul)
new_mul(4)
curry(curry)(5)
curry(curry)(1)(2)
(lambda x, y: mul(y, x))(3, 2)
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
28/43
Lambda Expressions (questions)SOLN
mul = lambda a, b: a * b
curry = lambda f: lambda x: lambda y: f(x, y)
new_mul = curry(mul)(3)
Eval to Outputs
curry(mul) FUNC FUNC
new_mul(4) 12 12
curry(curry)(5) FUNC FUNC
curry(curry)(1)(2) ERROR ERROR
(lambda x, y: mul(y, x))(3, 2) 6 6
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
29/43
Newton's Method (questions)
Wikipedia article
Write a function that returns the x-value at whicha local maximum or minimum occurs.
You may use approx_deriv(), andnewtons_method()
def find_max(fn, guess):
return______
http://en.wikipedia.org/wiki/Newtons_method7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
30/43
Newton's Method (questions) SOLN
Wikipedia article
Write a function that returns the x-value at whicha local maximum or minimum occurs.You may use approx_deriv(), andnewtons_method()
def find_max(fn, guess):
returnnewtons_method(lambda x: approx_deriv(fn),guess)
http://en.wikipedia.org/wiki/Newtons_method7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
31/43
Newton's Method (questions)
def newtons_method(fn, guess=1):
ALLOWED_ERROR_MARGIN = 0.0000001
while abs(fn(guess)) > ALLOWED_ERROR_MARGIN:
guess -= fn(guess) / deriv(fn, guess)return guess
True orFalse?
newtons_method will always terminate. fn is called 3 times in a single while loop.
removing abs could break the function
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
32/43
Newton's Method (questions) SOLN
def newtons_method(fn, guess=1):
ALLOWED_ERROR_MARGIN = 0.0000001
while abs(fn(guess)) > ALLOWED_ERROR_MARGIN:
guess -= fn(guess) / deriv(fn, guess)return guess
True orFalse?
newtons_method will always terminate. F fn is called 3 times in a single while loop. F
removing abs could break the function. T
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
33/43
Environment Diagrams
Show how Python executes code
Rules: http://www-inst.eecs.berkeley.
edu/~cs61a/sp13/pdfs/environment-diagrams.pdf
Online Python Tutor:http://inst.eecs.berkeley.edu/~cs61a-py/OnlinePythonTutor/v3/tutor.html
http://inst.eecs.berkeley.edu/~cs61a-py/OnlinePythonTutor/v3/tutor.htmlhttp://www-inst.eecs.berkeley.edu/~cs61a/sp13/pdfs/environment-diagrams.pdfhttp://inst.eecs.berkeley.edu/~cs61a-py/OnlinePythonTutor/v3/tutor.htmlhttp://inst.eecs.berkeley.edu/~cs61a-py/OnlinePythonTutor/v3/tutor.htmlhttp://www-inst.eecs.berkeley.edu/~cs61a/sp13/pdfs/environment-diagrams.pdfhttp://www-inst.eecs.berkeley.edu/~cs61a/sp13/pdfs/environment-diagrams.pdfhttp://www-inst.eecs.berkeley.edu/~cs61a/sp13/pdfs/environment-diagrams.pdf7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
34/43
Environment Diagrams (rules)
Assignment
1. Evaluate right-hand expression2. Write name in current frame
3. Bind expression to name
Function call
1. Draw frame (label p frame, intrinsic name)2. Bind formal parameters3. Evaluate function body
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
35/43
Environment Diagrams (rules)
Lookup
1. Check for name in current frame2. If not found, check in parent frame
3. If no parent, error
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
36/43
Environment Diagrams (rules)
DO NOT:
draw a new frame when defining functions
draw frames forbuilt-in functions
point variables to other variables
forget to label the frame's parent
forget to label the frame's intrinsic name
give Global a return value
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
37/43
Environment Diagrams (questions)
def mul(a, b):
if b == 1:return a
return a + mul(a, b - 1)
mul(3, 3)
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
38/43
Environment Diagrams (questions)SOLN
def mul(a, b):
if b == 1:return a
return a + mul(a, b - 1)
mul(3, 3)
http://bit.ly/151Qkrb
http://bit.ly/151Qkrbhttp://bit.ly/151Qkrb7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
39/43
Environment Diagrams (questions)
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)
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
40/43
Environment Diagrams (questions)SOLN
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)
http://bit.ly/WKB3WY
http://bit.ly/WKB3WYhttp://bit.ly/WKB3WYhttp://bit.ly/WKB3WY7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
41/43
Environment Diagrams (questions)
the = 4
def boom(goes):
def dynamite():
return boom(goes-1)
if goes < the:
return 9
goes += 4
return dynamite
the = boom(5)()
boom(10)
7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
42/43
Environment Diagrams (questions)SOLN
the = 4
def boom(goes):
def dynamite():
return boom(goes-1)
if goes < the:
return 9
goes += 4
return dynamite
the = boom(5)()
boom(10)
http://bit.ly/VMjpFM
http://bit.ly/VMjpFMhttp://bit.ly/VMjpFM7/29/2019 Berkeley CS 61A Spring 2013 Midterm 1 TA Review Session
43/43
Conclusion
Good luck! Don't panic, and you'll be fine. Questions?