def multiple(a, b):
"Return the smallest number n that is a multiple of both a and b.
> multiple(3, 4)
12
> multiple(14, 21)
42
"
factorsa = []
factorsb = []
for x in range (1,a+1,1):
if a%x = 0:
fact
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)
5
> a_plus_abs_b(2, -3)
5
"
if b < 0:
f = sub
else:
f = add
return f(a, b)
def two
Lecture 30: Mostly Project 4 Overview
Stream Example
f = Stream(1,
lambda: Stream(1,
lambda: combine_streams(add, f, f.rest)
This creates a new stream that initially contains only one item and a
lazy
Lecture #25: Calculator
A Sample Language: Calculator
Adminitrivia
Source: John Denero.
Extended TA office hours in labs Tuesday from 11AM.
Prefix notation expression language for basic arithmetic
Lecture #19: Complexity and Orders of Growth, contd.
The Notation
Suppose that f is a one-parameter function on real numbers.
O(f ): functions that eventually grow no faster than f :
g O(f ) means
Lecture #20: Tree Recursions, Memoization, Tree
Structures
Example: Escape from a Maze
Consider a rectangular maze consisting of an array of squares some
of which are occupied by large blocks of conc
Lecture #22: Search Trees and Sets, Part II
Adding (Adjoining) a Value
Must add values to a search tree in the right place: the place
tree_find would try to find them.
For example, if we add 17 to t
class Tree:
"A Tree consists of a label and a sequence
of 0 or more Trees, called its children."
def _init_(self, label, *children):
"A Tree with given label and children."
self._label = label
self._c
class Tree:
"A Tree consists of a label and a sequence
of 0 or more Trees, called its children."
def _init_(self, label, *children):
"A Tree with given label and children."
self._label = label
self._c
Lecture #21: Search Trees, Sets
General Tree Class (From Last Lecture)
class Tree:
"A Tree consists of a label and a sequence
of 0 or more Trees, called its children."
def _init_(self, label, *childre
class Tree:
"A Tree consists of a label and a sequence
of 0 or more Trees, called its children."
def _init_(self, label, *children):
"A Tree with given label and children."
self._label = label
self._c
Lecture #18: Complexity and Orders of Growth
A Direct Approach
Certain problems take longer than others to solve, or require more
storage space to hold intermediate results.
We refer to the time com
Lecture #23: Iterators on Trees
Slight Correction from Last Time
In the last lecture, I defined
class BinTree(Tree):
def _iter_(self): return tree_iter(self)
However, there is already an _iter_ meth
Lecture #26: Announcements
Project 1 revisions: due April 10.
Hackathon: There is a hackathon hosted by [email protected] Hackvs. Stanford this Weekend. Prizes include: Macbook Airs, Retina iPads, Occulus
Rifts,
Lecture #26: Announcements
Scheme and Scheme Interpretation
Project 1 revisions: due April 10.
Hackathon: There is a hackathon hosted by [email protected] Hackvs. Stanford this Weekend. Prizes include: Macbook A
Lecture 31: Declarative Programming
Imperative vs. Declarative
So far, our programs are explicit directions for solving a problem;
the problem itself is implicit in the program.
Declarative programm
"This module implements a declarative Logic language using Scheme syntax.
Logic is a variant of the Prolog language, based on scmlog by Paul Hilfinger
and the logic programming example in SICP.
All va
Lecture 29: Generators, Streams, and Lazy Evaluation
Classes of sequences
Some of the most interesting real-world problems in computer science center around sequential data.
We started with tuples a
CS61A Lecture #28: The Halting Problem and
Incompleteness
The Halting Problem
An interpreter (or compiler) is a program that operates on programs.
In fact, there are numerous other ways to operate o
Lecture 27: Anatomy of an Interpreter
Last modified: Mon Apr 7 13:57:29 2014
CS61A: Lecture #27
1
Interpreting Scheme
Your project will have a structure similar to the calculator:
Split input into t
Lecture #24: Programming Languages and Programs
Metalinguistic Abstraction
A programming language is a notation for describing computations
Weve created abstractions of actionsfunctionsand of things
Lecture #25: Calculator
Adminitrivia
Extended TA office hours in labs Tuesday from 11AM.
Exam is at 8PM on Wednesday; rooms to be assigned as happened
last time (not the same rooms: see postings and
class Range:
def _init_(self, low, high):
self._low = low
self._high = high
def _iter_(self):
return self._generate()
def _generate(self):
i = self._low
while i < self._high:
yield i
i += 1
class Lett
Lecture 27: Anatomy of an Interpreter
Interpreting Scheme
Your project will have a structure similar to the calculator:
Split input into tokens, which are themselves Scheme values.
Parse the tokens
def near(L, x, delta):
"True iff X differs from some member of sequence L by no
more than DELTA."
for y in L:
if abs(x-y) <= delta:
return True
return False
def is_unduplicated(L, pred):
"True iff the
from functools import reduce
from operator import add
def solve_maze(row0, col0, maze):
"Assume that MAZE is a rectangular 2D array (list of lists) where
maze[r][c] is true iff there is a concrete blo
Lecture #9: More Functions
Another Tree Recursion: Hog Dice
What are the odds of rolling at least k in hog with n s-sided dice?
(n > 0 and for us, s > 0 is 4 or 6)
# rolls of n s-sided dice totaling