Slides08
Slides08
Part III
Outline for Today
●
Iteration + Recursion
●
Combining two techniques together.
●
Enumerating Permutations
●
What order should we do things?
●
Enumeration, Generally
●
How to think about enumeration problems.
More On Self-Similarity
This
Thisself-similar
self-similar
shape
shapeis
iscalled
calledaa
Sierpinski
Sierpinski
carpet.
carpet.
An
Anorder-0
order-0
Sierpinski
Sierpinskicarpet
carpet
is
isaafilled
filledsquare.
square.
Otherwise,
Otherwise,aa
Sierpinski
Sierpinskicarpet
carpetisis
eight
eightsmaller
smallercarpets
carpets
arranged
arrangedininthis
thisgrid
grid
pattern.
pattern.
(0, 0) (0, 1) (0, 2)
(1, 0) (1, 2)
(1, 0) (1, 2)
(2,
We can visit each
0) (2, 1) (2, 2)
We can visit each
spot
spotwith
withaadouble
double
forloop.
for loop.
Iteration + Recursion
●
It’s completely reasonable to mix iteration
and recursion in the same function.
●
Here, we’re firing off eight recursive calls,
and the easiest way to do that is with a
double for loop.
●
Recursion doesn’t mean “the absence of
iteration.” It just means “solving a
problem by solving smaller copies of that
same problem.”
Time-Out for Announcements!
Assignment 2
●
Assignment 2 was due today at the start
of lecture.
●
The grace period for late submissions
ends this Sunday at 11:30AM Pacific
time.
●
Have questions? Ask them on EdStem, or
email your section leader!
Assignment 3
●
Assignment 3 (Recursion!) goes out today. It’s due
next Friday at the start of class.
●
Play around with recursion and recursive problem-solving!
●
This assignment may be completed in pairs.
Some reminders:
●
You are not required to work in a pair. It’s totally fine to
work independently.
●
If you do work in a pair, you must work with someone else
in your discussion section.
●
Work together, not separately. Doing only half the
assignment teaches you less than half the concepts.
Working collaboratively and interactively with your partner
will improve your learning outcomes.
(The Curtain Rises on Act II)
Enumerating Permutations
A permutation is a rearrangement
of the elements of a sequence.
Lassen Volcanic National Park Yosemite National Park
HI AI AH
H I A I A H
"A" "H" "I"
I H I A H A
"AH" "AI" "HA" "HI" "IA" "IH"
I H I A H A
H? H?
{A} {}
✓ × ✓ ×
I? I? I? I?
{A, H} {A} {H} {}
✓ × ✓ × ✓ × ✓ ×