EECS 70
Spring 2013
Discrete Mathematics and Probability Theory
Anant Sahai
HW 8
Due March 18
1. Deja Vu
Gandalf has this habit of pacing back and forth when he is in deep thought. Feeling a bit concerned, Bilbo
claims that this behavior will eventually g
S CHEME
11
C OMPUTER S CIENCE 61A
October 29th, 2012
In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually write a
Scheme interpreter in Project 4.
E XCEPTIONS , C ALCULATOR
10
C OMPUTER S CIENCE 61A
November 5th, 2012
We are beginning to dive into the realm of interpreting computer programs - that is, writing programs that understand programs. In order to do so, well have to examine programming lang
TAIL R ECURSION , S COPE , AND P ROJECT 4
11
C OMPUTER S CIENCE 61A
Noveber 12, 2012
1
Tail Recursion
Today we will look at Tail Recursion and Tail Call Optimizations in Scheme, and how
they relate to iteration in Python. Lets start with an example:
From
I TERATORS , G ENERATORS , AND S TREAMS
12
C OMPUTER S CIENCE 61A
November 19, 2012
1
Introduction
Innite sequences frequently arise in computer science. For example, the sequence of
all natural numbers is an innite sequence, because there is no last natu
L OGIC P ROGRAMMING
13
C OMPUTER S CIENCE 61A
November 26, 2012
1
Introduction
Over the semester, we have been using imperative programming a programming style
where code is written as a set of instructions for the computer. In this section, we introduce
"61A Homework 2
Name:
Login:
TA:
Section:
"
def square(x):
"Return x squared."
return x * x
# Q1.
def product(n, term):
"Return the product of the first n terms in a sequence.
term - a function that takes one argument
> product(4, square)
576
"
to
"61A Homework 5
Name:
Login:
TA:
Section:
"
# Q1.
def count_calls(f):
"A function that returns a version of f that counts calls to f and can
report that count to how_many_calls.
The returned function responds to a special string argument,
'how many ca
"61A Homework 6
Name:
Login:
TA:
Section:
"
# Q1.
class VendingMachine(object):
"A vending machine that vends some product for some price.
> v = VendingMachine('crab', 10)
> v.vend()
'Machine is out of stock.'
> v.restock(2)
'Current crab stock: 2'
"61A Homework 8
Name:
Login:
TA:
Section:
"
def empty(s):
return len(s) = 0
def set_contains2(s, v):
"Return true if set s contains value v as an element.
> set_contains2(s, 2)
True
> set_contains2(s, 5)
False
"
if empty(s) or s.first > v:
return
# Name:
# Login:
# TA:
# Section:
# Q1.
def num_splits(s, d):
"Return the number of ways in which s can be partitioned into two
sublists that have sums within d of each other.
> num_splits(cfw_1, 5, 4, 0) # splits to cfw_1, 4 and cfw_5
1
> num_splits
R LISTS , T REES , AND O RDERS OF G ROWTH
8
C OMPUTER S CIENCE 61A
October 22, 2012
1
Recursive Lists
Weve already seen Rlists implemented as recursive pairs, and weve drawn box-andpointer representing their structure. What well go through today is an obj
R ECURSION
7
C OMPUTER S CIENCE 61A
October 15, 2012
1
Recursion
We say a procedure is recursive if it calls itself in its body. Below is an example of a
recursive procedure to nd the factorial of a positive integer n:
def factorial(n):
if n = 0 or n = 1:
EECS 70
Spring 2013
Discrete Mathematics and Probability Theory
Anant Sahai
HW 9
Due April 1
1. Introductions
Its the rst discussion section and the GSI is trying to come up with a clever method to make students form
groups and introduce themselves to eac
EECS 70
Discrete Mathematics and Probability Theory
Spring 2013
Anant Sahai
MT 1 Solution
P RINT your student ID:
,
P RINT AND S IGN your name:
(last)
(rst)
(signature)
P RINT your Unix account login: cs70P RINT your discussion section and GSI:
Mark Here
EECS 70
Discrete Mathematics and Probability Theory
Spring 2013
Anant Sahai
1. (2 pts.)
Soln 1
Getting started
What is Anant Sahais second favorite number?
The answer is found on Piazza.
(Why are we having you do this? Piazza is your best source for recen
EECS 70
Discrete Mathematics and Probability Theory
Spring 2013
Anant Sahai
Soln 2
1. (4 pts.) Proof by induction
For n N with n 2, dene sn by
sn = 1
1
1
1
1
1
.
2
3
n
Prove that sn = 1/n for every natural number n 2.
Answer: We will use induction on n
EECS 70
Discrete Mathematics and Probability Theory
Spring 2013
Anant Sahai
Soln 3
1. Simple recurrence relations
Assume that you have a sequence of integers dened by an initial condition and a recursive relation. In each
case nd a simple expression that
EECS 70
Discrete Mathematics and Probability Theory
Spring 2013
Anant Sahai
Soln 4
Due Feb 18
1. Modulo arithmetic practice
1. Use Euclids algorithm (page 3 of note 5) to compute the greatest common divisor of 527 and 323.
List the values of x and y of al
EECS 70
Discrete Mathematics and Probability Theory
Spring 2013
Anant Sahai
Soln 5
Due Feb 25
1. Polynomials and modular arithmetic
Which of the following statements is true? In each case, if the statement is true give a brief explanation; if
it is false,
EECS 70
Discrete Mathematics and Probability Theory
Spring 2013
Anant Sahai
Soln 6
Due Mar 4
1. d + 2 points vs. a polynomial of degree d
1. Given 3 points (0, 1), (1, 1), and (2, 3), use Lagrange interpolation to construct the degree-2 polynomial
which g
EECS 70
Discrete Mathematics and Probability Theory
Spring 2013
Anant Sahai
Soln 7
1. Error-Detecting Codes
In the realm of error-correcting codes, we usually want to recover the original message if we detect any
errors, and we want to provide a guarantee
E XPRESSIONS , S TATEMENTS , AND F UNCTIONS
1
C OMPUTER S CIENCE 61A
September 4, 2012
0.1 Warmup What Would Python Do?
> x = 6
> def square(x):
.
return x * x
> square(x)
Solution: 36
> max(pow(2, 3), square(-5) - square(4)
Solution: 9
1
Expressions
Expr
H IGHER O RDER F UNCTIONS
2
C OMPUTER S CIENCE 61A
September 10, 2012
1
Warmup Questions
1. Here is one method to check if a number is prime:
def is_prime(n):
k=2
while k < n:
if n % k = 0:
return False
k += 1
return True
How does this function work?
Solu
N EWTON S M ETHOD AND S EQUENCES
3
C OMPUTER S CIENCE 61A
September 17, 2012
1
Sequences
In Fridays lecture, you were introduced to sequences. Today, we are going to cover a few
ways we can model and create sequences. A sequence is an ordered collection o