University of California, Berkeley
EECS Department - Computer Science Division
CS3 Lecture 11 : We Take An Exam Together
Overview of today's lecture
Review
Recursion
- We've seen several different kinds of recursion, from embedded
to tail to advanced.
We Take An Exam Together
Overview
- Use the exam from Spring 1994 -- Only questions 2 and 3 are
relevant for this exam
- Old exams don't test advanced recursion as much; they instead
focus on lists and trees when they ask their advanced recursion
questions.
- Here are more advanced recursion questions:
Advanced Recursion: grow-mountains
- Write a procedure grow-mountains, that, given n,
returns a sentence of length n, where each word is a
"mountain". A mountain is a word in which the letters
count up from 1 to i and down to 1
again (...and i increases from 1 to n).
: (grow-mountains 0)
()
: (grow-mountains 4)
(1 121 12321 1234321)
Advanced Recursion: binary
- Write binary, a procedure to generate the possible
binary numbers given n bits.
- Each "bit" is a 0 or 1.
- You can think of this as the enumeration of all the possible
ways a set of n light switches could be configured (0
= off, 1 = on)
: (binary 1)
(0 1)
: (binary 2)
(00 01 10 11)
: (binary 3)
(000 001 010 011 100 101 110 111)
Advanced Recursion: strip-most-popular-letter
- Write strip-most-popular-letter, which removes the
most common letter from every word in the given sentence
: (strip-most-popular-letter '(cs3 is the best class))
(c3 i th bet cla)
Summary
- Today we took an exam together in preparation for your evening
midterm and talked about the project a bit
Next Time
Puzzle : Shuffling cards
- You've already been asked to find out how many shuffles (described
in homework 9) it takes until a deck of 52 cards are the same
order again.
- Generalize this to find a mathematical function that describes
how many shuffles it would take a deck of n cards (n
has to be even, btw) to be the same order again.
- E.g., When n = 2, they are the same after 1 shuffle
: (shuffle '(1 2)) ==> (1 2)
- E.g., When n = 4 they are the same after 2 shuffles
: (shuffle '(1 2 3 4)) ==> (1 3 2 4)
: (shuffle '(1 3 2 4)) ==> (1 2 3 4)
Matchboxes ["Pentagames" by Pentagram,
Fireside Publishing, 1990]
- Make the largest possible grid using the matches from a large
box.
- Two players take in turn to remove one match or two adjacent
matches.
- Adjacent can mean in the same line and touching ends
- or Adjacent can mean in the same box (neighbors)
- or both, depending on how you want to play...
- The winner is the player who removes the last match.
| | |
- - - -
| | |
- - - -
| | |
- - - -
| | |