Homework 7 Solutions
Question 1: Digits
"Return the digits of n as a lin
"Lab 1: Expressions and Control Structures"
# Coding Practice
def repeated(f, n, x):
"Returns the result of composing f n times on x.
> def square(x):
return x * x
> repeated(square, 2, 3) # square(square(3), or 3 * 4
> repeated(square, 1, 4) # squ
"Returns the time required to execute FUNC() in seconds."
t0 = time.perf_counter()
return time.perf_counter() - t0
def missing_val(lst0, lst1):
"Assuming that lst0 contains all the values in lst1, but lst1 is missing
from operator import add, sub
def a_plus_abs_b(a, b):
"Return a+abs(b), but without calling abs.
> a_plus_abs_b(2, 3)
> a_plus_abs_b(2, -3)
if b < 0:
#assigne the sub function to f
f = add
"Lab 2: Higher Order Functions & Lambdas & Recursions"
# Lambda Functions
Returns a Curried version of a two-argument function FUNC.
> from operator import add
> curried_add = lambda_curry2(add)
> add_three = curried_add(3)
61A Lecture 5
Office Hours: You Should Go!
You are not alone!
Environments for Higher-Order Functions
Environments Enable Higher-Order Functions
Functions are first-class: Functions are values in our progr
61A Lecture 7
Hog Contest Rules
Fall 2011 Winners
Slight rule changes
Your score is the number
Yan Duan & Ziming Li
Brian Prike & Zhenghao Qian
Parker Schuh & Robert Chatham
to two people submit one entry;
Max of one entry
CS 61A Lecture 10
Working with Lists
> digits = [1, 8, 2, 8]
> digits = [2/2, 2+2+2+2, 2, 2*2*2]
The number of elements
An element selected by its index
> getitem(digits, 3)
Concatenation and re
Homework 11 Solutions
Question 1: In Order
Yields the entries of a valid binary search t
"The Game of Hog."
from dice import four_sided, six_sided, make_test_dice
from ucb import main, trace, log_current_line, interact
GOAL_SCORE = 100 # The goal of Hog is to score 100 points.
# Phase 1: Simulator #
def roll_dice(num_rolls, dice=six_sided
61A Lecture 9
A date: a year, a month, and a day
A geographic position: latitude and longitude
Compound values combine other values together
Data abstraction lets us manipulate compound v
CS 61A Lecture 11
The Closure Property of Data Types
method for combining data values satisfies the closure property if:
The result of combination can itself be combined using the same method
is powerful b
61A Lecture 8
return mul(x, x)
def sum_squares(x, y):
return square(x) + square(y)
What does sum_squares need to know about square?
Square takes one argument.
Square has the intrinsic name
Lecture #11: Strings, Mutable Data
Strings: A Specialized Type of Sequence
Strings are sequences of characters, with a good deal of special
Rather odd property: the base cases are circular. Characters are
themselves strings of length 1!
Welcome to CS61A!
This is a course about programming, which is the art and science of
constructing artifacts (programs) that perform computations or
interact with the physical world.
To do this, we have to learn a programming language (Python
Lecture #15: OOP
From Last Time
Just as def defines functions and allows us to extend Python with
new operations, class defines types and allows us to extend Python
with new kinds of data.
The Account type illustrated how we do each of these
CS 61A Midterm 1 Study Guide Page 1
mul(add(2, mul(4, 6), add(3, 5)
add(2, mul(4, 6)
A name is bound to a value
Red arrow points to next
CS 61A Midterm 2 Study Guide Page 1
nonlocal <name> , <name 2>, .
Effect: Future assignments to that name change its
pre-existing binding in the first non-local frame of
the current environment in which that name is bound.
Tuples are immutable sequences.
Lecture #16: (Mostly) Interfaces and Generic Functions
Using Base Types
Sometimes, we want an overriding method in a subtype to augment
rather than totally replace an existing method.
That means that we have to call the original version of the method
61A Lecture 6
Definition: A function is called recursive if the body of that function calls itself,
either directly or indirectly
Implication: Executing the body of a recursive function may require app
61A Lecture 12
consist of data and behavior, bundled together to create abstractions
can represent things, but also properties, interactions, & processes
type of object i