Problem-Solving with Karel
Announcements
● Programming Assignment #1 Out:
● Karel the Robot: Due Friday, January 20 at
3:15 PM
● Email: Due Sunday, January 22 at 11:59PM
● Sign up for section!
http://cs198.stanford.edu/section
Signups close this Sunday at 5PM.
Friday Four Square
● Good snacks!
● Good company!
● Good game!
● Good fun!
● Today at 4:15
in front of
Gates.
Don't be this guy!
Revisiting HurdleJumpingKarel
RowSweepingKarel
Before
1 . . . . . . . .
1 2 3 4 5 6 7 8
After
1 . . . . . . . .
1 2 3 4 5 6 7 8
Let's Code it Up!
Karel has to take six steps…
1 . . . . . . .
1 2 3 4 5 6 7
…but has to sweep seven corners.
A More Elaborate Problem
8 . . . . . . . .
7 . . . . . . . .
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 . . . . . . . .
1 . . . . . . . .
1 2 3 4 5 6 7 8
RoombaKarel
The Problem
● Setup:
● Karel begins at (1, 1).
● Karel's world has no walls in it.
● Each corner has zero or one beepers.
● Goal:
● Karel's world is free of beepers.
● Karel's location does not matter.
8 . . . . . . . .
7 . . . . . . . .
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 . . . . . . . .
1 . . . . . . . .
1 2 3 4 5 6 7 8
8 . . . . . . . .
7 . . . . . . . .
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 . . . . . . . .
1 . . . . . . . .
1 2 3 4 5 6 7 8
8 . . . . . . . .
7 . . . . . . . .
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 . . . . . . . .
1 . . . . . . . .
1 2 3 4 5 6 7 8
8 . . . . . . . .
7 . . . . . . . .
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 . . . . . . . .
1 . . . . . . . .
1 2 3 4 5 6 7 8
8 . . . . . . . .
7 . . . . . . . .
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 . . . . . . . .
1 . . . . . . . .
1 2 3 4 5 6 7 8
Algorithms
● An algorithm is a process for solving
some problem.
● Named for 9th-century Persian
mathematician محمد بن موسى الخوارزمي,
(Muhammad ibn Musa al-Khwarizmi).
● There are many algorithms for solving
each problem; each offers tradeoffs.
8 . . . . . . . .
7 . . . . . . . .
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 . . . . . . . .
1 . . . . . . . .
1 2 3 4 5 6 7 8
BeeperDoublingKarel
Before
1 . .
137 .
1 2 3
After
1 . .
274 .
1 2 3
How do you double beepers?
1 . 3. .
1 2 3
1 . 2. 2.
1 2 3
1 . 1. 4.
1 2 3
1 . . 6.
1 2 3
1 . 6. .
1 2 3