27 views

Original Title: Midterm 1 Review Solution

Uploaded by tehkronos

- Mathematics
- Calculus Workbook
- Sol Ingles Enero 12
- MTE-01_E_2013
- Thomas Calculus Review for Final Examt
- Fixit
- Sitnikov Newton
- Chapter3 Handout
- Newton-Raphson-Method.pdf
- 4.Deep Learning Assignment4 Solution
- Worksheet Minmax Soln
- Bee3143 Assignment 3 Fazrul Ec15131
- 2014Fall_UNIST01_LecIntro
- Chapter5 A
- 111107062
- sec_1-3
- AP CALCULUS Curriculum Module: Extrema
- CH1
- NM Lecture 0 on 25th July 2017.pdf
- twenty festivals rules.alderac.pdf

You are on page 1of 9

September 15, 2012

All relevant midtermmaterial can be found on the course website under Announcements,

in the link to Midterm 1.

Disclaimer: This reviewsession is not meant to be exhaustive. While the material covered

during the session is important, there might be material that we cant get to that is equally

important. For the exhaustive list of possible material for the exam, refer to the website.

0.1 Warmup

1. Figure out what this code is supposed to do and correct it:

>>> def nondescriptive_name(n):

... k = 1

... while k <= n:

... if n % k == 0:

... print(k, n//k)

... k += 1

... return total

Solution: Move k += 1 inside the while loop. Also, this function will print out

redundant factors, so you only have to go up to the square root of n.

2. The factorial of a number n is the total product of all numbers before it, down to 1.

That is, 7! is 7 6 5 4 3 2 1. Write prime factorial which only multiplies the

prime numbers before n. That is, 7! is 7 5 3 2. Assume you are given the function

is prime.

def prime_factorial(n):

1

DISCUSSION 1: MIDTERM 1 REVIEW SHEET Page 2

Solution:

k, total = 2, 1

while k <= n:

if is_prime(k):

total

*

= k

k += 1

return total

1 What Would Python Do?

1. Recall accumulate fromhomework, which takes in combiner, start n and term.

>>> from operator import mul

>>> accumulate(mul, 1, 4, lambda x: pow(x, 2))

Solution: Returned: 576

2. Recall the compose function from lecture, reproduced below. Assume that square

and cube are given, and behave as expected.

>>> def compose(f, g):

... def h(x):

... return f(g(x))

... return h

>>> compose(compose(print, square), cube)(3)

Solution: Printed: 729 Returned: None

3. Its a lambda free-for-all!

>>> tic = lambda num: lambda y: num + y

>>> tac, toe = 10, 20

>>> tic(tac)(toe)

Solution: 30

CS61A Fall 2012: John Denero, with

Akihiro Matsukawa, Hamilton Nguyen, Phillip Carpenter, Steven Tang, Varun Pai, Joy Jeng, Keegan

Mann, Allen Nguyen, Stephen Martinis, Andrew Nguyen, Albert Wu, Julia Oh, Shu Zhong

DISCUSSION 1: MIDTERM 1 REVIEW SHEET Page 3

2 Higher Order Functions

1. Try to gure out what Python would print at the interpreter:

>>> f = lambda x: lambda y: x + y

>>> f

Solution: < function < lambda > ... >

>>> f(4)

Solution: < function < lambda > ... >

>>> f(4)(5)

Solution: 9

>>> f(4)(5)(6)

Solution: Error: int type is not callable

2. Dene a function g such that the following returns 3:

>>> g(3)()

3

def g(n):

Solution:

g = lambda x: lambda: 3

3. Smoothing a function is an important concept in signal processing. If f is a function

and dx is some small number, then the smoothed version of f is the function whose

value at a point x is the average of f(x-dx), f(x) and f(x+dx). Write a function

smooth that takes as input a function f and a number dx and returns a function that

computes the smoothed f.

CS61A Fall 2012: John Denero, with

Akihiro Matsukawa, Hamilton Nguyen, Phillip Carpenter, Steven Tang, Varun Pai, Joy Jeng, Keegan

Mann, Allen Nguyen, Stephen Martinis, Andrew Nguyen, Albert Wu, Julia Oh, Shu Zhong

DISCUSSION 1: MIDTERM 1 REVIEW SHEET Page 4

def smooth(f, dx):

Solution:

def smoothed(x):

return (f(x- dx) + f(x) + f(x + dx)) / 3

return smoothed

3 Environment Diagrams

1. Draw the environment diagram

>>> p, q = 3, 6

>>> def foo(f, p, q):

... return f()

>>> foo(lambda: pow(p, q), 2, 4)

CS61A Fall 2012: John Denero, with

Akihiro Matsukawa, Hamilton Nguyen, Phillip Carpenter, Steven Tang, Varun Pai, Joy Jeng, Keegan

Mann, Allen Nguyen, Stephen Martinis, Andrew Nguyen, Albert Wu, Julia Oh, Shu Zhong

DISCUSSION 1: MIDTERM 1 REVIEW SHEET Page 5

2. Draw the remaining environment diagram after the last call and ll in the blanks.

>>> from operator import add, sub

>>> def burrito(x, y):

... return add(x, y)

...

>>> sandwich = (lambda f: lambda x, y: f(x, y))(add)

>>> add = sub

>>> burrito(3, 2)

>>> sandwich(3, 2)

CS61A Fall 2012: John Denero, with

Akihiro Matsukawa, Hamilton Nguyen, Phillip Carpenter, Steven Tang, Varun Pai, Joy Jeng, Keegan

Mann, Allen Nguyen, Stephen Martinis, Andrew Nguyen, Albert Wu, Julia Oh, Shu Zhong

DISCUSSION 1: MIDTERM 1 REVIEW SHEET Page 6

4 Newtons Method and Iterative Improvement

1. In this problem, rather than nding zeros of functions, we will attempt to nd the

minima of functions. As we will see, both Newtons method and different iterative

improvement method can be used to solve this problem. You can assume that your

inputs are functions with exactly one minimum and no maximum (known as convex

functions).

Before we discuss a specic iterative improvement algorithmto nd a local minimum,

lets rst consider the problem in general. What should be the terminating condition

(i.e. the done function) be for this minimization problem?

Solution: f(x

) f(x) 0

2. You learned in calculus that the minima of a function occur at zeros of the rst deriva-

tive. Hey! We already know how to nd derivatives and zeros! Here are the relevant

functions:

def iter_improve(update, done, guess=1, max_updates=1000):

"""Iteratively improve guess with update until

done returns a true value.

"""

k = 0

while not done(guess) and k < max_updates:

guess = update(guess)

k = k + 1

return guess

def approx_derivative(f, x, delta=1e-5):

"""Return an approximation to the derivative of f at x."""

df = f(x + delta) - f(x)

return df/delta

def find_root(f, guess=1):

"""Return a guess of a zero of the function f, near guess.

"""

return iter_improve(newton_update(f), lambda x: f(x) == 0, guess)

Use them to nd the minimum of f by solving for the root of its derivative!

def find_minimum(f, guess=1):

""" Return a guess of a zero of the function f, near guess.

CS61A Fall 2012: John Denero, with

Akihiro Matsukawa, Hamilton Nguyen, Phillip Carpenter, Steven Tang, Varun Pai, Joy Jeng, Keegan

Mann, Allen Nguyen, Stephen Martinis, Andrew Nguyen, Albert Wu, Julia Oh, Shu Zhong

DISCUSSION 1: MIDTERM 1 REVIEW SHEET Page 7

Solution:

return iter_improve(newton_update(approx_derivative(f), \

lambda x: f(x) == 0, guess)

Note: the \ symbol means continue the code

on a new line

3. There is a very commonly used iterative improvement algorithmused to nd the clos-

est local minimumof a function, known as gradient descent. The idea behind gradient

descent is to take a tiny step at each point towards the direction of the minimum. How

the direction and size of this step is is a (small) constant factor of the derivative. Here

is an illustration of what it does:

Since this is not a math class, well make your life easier by giving you the update

equation, for some small :

x

= x f

(x)

Given all of this, implement the gradient descent algorithm by lling in the skeleton

below. You may use the functions given in (2).

Hint: To check for termination, your current guess must remember the old value

you were just at. Therefore, your guess should be a tuple of 2 numbers and you

should update it like (x, x

) (x

, x

).

def gradient_done(f, size = pow(10, -6)):

""" Return a function to evaluate whether our answer is

good enough, that is, when the change is less than size.

"""

Solution:

def done(guess):

before = guess[0]

CS61A Fall 2012: John Denero, with

Akihiro Matsukawa, Hamilton Nguyen, Phillip Carpenter, Steven Tang, Varun Pai, Joy Jeng, Keegan

Mann, Allen Nguyen, Stephen Martinis, Andrew Nguyen, Albert Wu, Julia Oh, Shu Zhong

DISCUSSION 1: MIDTERM 1 REVIEW SHEET Page 8

if before == None:

return False

after = guess[1]

return abs(f(after)-f(before)) < size

return done

def gradient_step(f, alpha=0.05):

""" Return an update function for f using the

gradient descent method.

"""

Solution:

def update(x):

current = x[1]

return (current, current - alpha

*

\

approx_derivative(f, current))

return update

def gradient_descent(f, guess=1):

""" Performs gradient descent on the function f,

starting at guess"""

guess = (None, guess)

Solution:

return iter_improve(gradient_step(f), \

gradient_done(f), guess)[1]

5 Data Abstraction

1. Consider the following data abstraction for 2-element vectors:

def make_vect(xcor, ycor):

return (xcor, ycor)

def xcor(vect):

return vect[0]

def ycor(vect):

CS61A Fall 2012: John Denero, with

Akihiro Matsukawa, Hamilton Nguyen, Phillip Carpenter, Steven Tang, Varun Pai, Joy Jeng, Keegan

Mann, Allen Nguyen, Stephen Martinis, Andrew Nguyen, Albert Wu, Julia Oh, Shu Zhong

DISCUSSION 1: MIDTERM 1 REVIEW SHEET Page 9

return vect[1]

Change this denition of vector sum to not have abstraction barrier violations.

def vector_sum(vect_tuple):

xsum, ysum, i = 0, 0, 0

while i < len(vect_tuple): # len returns length of tuple

xsum = vect_tuple[i][0] + xsum

ysum = vect_tuple[i][1] + ysum

return xsum, ysum

Solution:

def vect_sum(vect_tuple):

xsum = 0

ysum = 0

i = 0

while i < len(vect_tuple):

xsum = xcor(vect_tuple[i]) + xsum

ysum = ycor(vect_tuple[i]) + ysum

i += 1

return make_vect(xsum, ysum)

2. Suppose we redene make vect as follows:

def make_vect(xcor, ycor):

vect = lambda f: f(xcor, ycor)

return vect

What functions would we need to change? Go ahead and redene them below.

Solution:

def xcor(vect):

return vect(lambda x, y: x)

def ycor(vect):

return vect(lambda x, y: y)

CS61A Fall 2012: John Denero, with

Akihiro Matsukawa, Hamilton Nguyen, Phillip Carpenter, Steven Tang, Varun Pai, Joy Jeng, Keegan

Mann, Allen Nguyen, Stephen Martinis, Andrew Nguyen, Albert Wu, Julia Oh, Shu Zhong

- MathematicsUploaded byPaul Immanuel Ladera
- Calculus WorkbookUploaded byAdrian Kibet Bob
- Sol Ingles Enero 12Uploaded byMiguel Tejada Méndez
- MTE-01_E_2013Uploaded bySujayChowdhury
- Thomas Calculus Review for Final ExamtUploaded byBrendan McWilliams
- FixitUploaded bySonya Bajwa
- Sitnikov NewtonUploaded byNono4138
- Chapter3 HandoutUploaded byKim Uyên Nguyen Ngoc
- Newton-Raphson-Method.pdfUploaded byGerardo Alfredo Encarnacion Ramirez
- 4.Deep Learning Assignment4 SolutionUploaded byvidhya ds
- Worksheet Minmax SolnUploaded bySam Fraser
- Bee3143 Assignment 3 Fazrul Ec15131Uploaded byFazrul Chew
- 2014Fall_UNIST01_LecIntroUploaded byTae Hoon Kim
- Chapter5 AUploaded bySulaiman Dawood Barry
- 111107062Uploaded byEtta Amaresh
- sec_1-3Uploaded byCada Mideh
- AP CALCULUS Curriculum Module: ExtremaUploaded byteachopensource
- CH1Uploaded byprasaad08
- NM Lecture 0 on 25th July 2017.pdfUploaded byRishabh Khandelwal
- twenty festivals rules.alderac.pdfUploaded byjohnson12mr
- Unified Power Flow ControllerUploaded bysirageldeen
- Thermal ActionsUploaded byVladislavs Pozņaks
- Cf d 2007 TorontoUploaded byMohamed Hassani
- A Simple Algorithm for Unbalanced Radial Distribution System Load FlowUploaded byMarcelo Freitas
- 12a.ipho_BUL81The.docUploaded byfienny37
- AodUploaded byAditya Bansal
- 209 Formula SheetUploaded byAlan Choy
- Calculus - Complete Book on Calculus 1,2 and 3 - Paul DawkinsUploaded byvincepay
- The Antifragility SurveyUploaded byRaymundo Soto
- Chapter 6 from Power System Analysis - Hadi Saada with answerst.pdfUploaded byطلال النسري

- MATH 54 - Fall 1996 - Rezakhanlou - Midterm 1Uploaded bytehkronos
- Math53 Sp04 Mt1 Graber SolnUploaded bytehkronos
- MATH 53 - Fall 2003 - Hare - Midterm 1Uploaded bytehkronos
- CS 170 Lecture 20 HandoutUploaded bytehkronos
- mt1reviewUploaded bytehkronos
- Math54 Teleman Final 2011Spring SolnUploaded bytehkronos
- Chap 1 NotesUploaded bytehkronos
- MIT6.01Uploaded bytehkronos
- Spanish Script MarcoUploaded bytehkronos
- English Finals Study GuideUploaded bytehkronos
- 1st Sem Syllabus AP Euro Fall 2010 Old BookUploaded bytehkronos

- DecodeUploaded byapi-3716519
- ManualUploaded byarsalansaleem
- A 76940Uploaded bygmanikanta5
- Lecture - 16 Data Flow and Systolic Array Architectures.pdfUploaded bymohsinmanzoor
- f[1]Uploaded bybellahanisa65
- MuddyWater Operations in Lebanon and OmanUploaded byqpr80842
- les08Uploaded byTung Pham
- 7 1 1 WebMethods Developer Users GuideUploaded bypearl008
- Interview Question for ExperienceUploaded byRavi Srivastav
- 14_mem_mgmtUploaded byAbey Ravi
- chap06Uploaded byVP Standin Syahir
- Error Parsing Near PDF 1 3Uploaded byJill
- SAP ABAP Hard Coding Angels and DemonsUploaded bylalitsachdeva
- QTP 90 ExercisesUploaded byKiran Kumar
- Assignments for Device Drivers Programming in LinuxUploaded bySahil Khan
- 2839249-dzone-rc-getting-started-with-git.pdfUploaded byess_kay
- Intel IPP Samples for Windows - работа над ошибкамиUploaded byTanyaZaxarova
- SAS Case StudyUploaded byHarsh Chauhan
- Lecture - Stack ADT.pdfUploaded byCindy Jones
- C Course Imperial CollegeUploaded byisampalis
- Unix_cmdUploaded byswagatpttnk
- JavaLabBook_Lab1Sem2Uploaded byMayuresh Ghodke
- Mongo_DBUploaded byMohit Gupta
- D2CManualUploaded bykarzthik
- 9608_s15_ms_41Uploaded bysahar
- Papyrus TutorialUploaded byGonzalo Maurelia
- Getting Started With SVEditor v1.5Uploaded byChristopher Patterson
- CANdriver Lib DocuUploaded byAymen Ben
- History of Educational Technology 1210521877967329 8Uploaded byErick Monte
- Linked List Based Algo'sUploaded byJidav