Lecture3 Note
Lecture3 Note
Week 2
Fall 2022
Instruction set
Arithmetic and logical operations
+, -, *, /, and ** for defining
and, or, not expressions
Assignment
Conditionals
Iterations
Input/output
No pointers
No declarations
CSE 1101 2
AST
U
CSE 1061 3
OUTLINE AST
U
Conditionals
Iterations
for_loops
while_loops
CSE 1061 4
CONDITIONALS AST
U
if <condition> :
True block
else:
False block
CSE 1061 6
AST
U
CSE 1101 7
SENSING A BEEPER AST
U
CSE 1061 8
AST
U
if not hubo.on_beeper():
hubo.drop_beeper()
CSE 1061 10
FOLLOWING THE BOUNDARY AST
U
def move_or_turn():
if hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
def dance():
for i in range(4):
hubo.turn_left()
def move_or_turn():
if hubo.front_is_clear():
dance()
hubo.move()
else:
hubo.turn_left()
hubo.drop_beeper()
For i in range(18):
move_or_turn() ## Ex: 1
dancing.py CSE 1061 12
MULTIPLE CHOICES AST
U
if hubo.on_beeper():
hubo.pick_beeper()
else:
elif hubo.front_is_clear():
if hubo.front_is_clear():
hubo.move()
hubo.move()
elif hubo.left_is_clear(): else:
hubo.turn_left() if hubo.left_is_clear():
elif hubo.right_is_clear(): hubo.move()
turn_right() ---------------------------
else:
turn_around()
CSE 1061 13
MULTIPLE CHOICES AST
U
CSE 1061 15
AST
U
CSE 1061 16
AST
U
hubo.drop_beeper()
hubo.move() Why this?
while not hubo.on_beeper():
if hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
hubo.turn_left()
CSE 1061 18
AST
U
How about this case?
hubo.drop_beeper()
hubo.move()
while not
hubo.on_beeper():
if
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
hubo.turn_left()
hubo.drop_beeper()
hubo.move()
while not hubo.on_beeper():
if hubo.right_is_clear():
turn_right()
hubo.move()
elif
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
Does this always
CSE 1061 work? 21
AST
U
CSE 1061 22
AST
U
Hubo.turn_left()
CSE 1061 23
AST
U
CSE 1061 24
AST
U
hubo.drop_beeper()
while not
hubo.front_is_clear(): def
hubo.turn_left() mark_and_move():
hubo.move()
while not
hubo.on_beeper(): def follow_wall():
if hubo.right_is_clear():
turn_right() mark_and_move()
hubo.move()
while not
elif
hubo.on_beeper():
hubo.front_is_clear():
hubo.move() follow_wall()
else: hubo.turn_left()
hubo.turn_left()
Hubo.turn_left() CSE 1061 25
COMMENTS FOR HUMANS AST
U
One of the secrets of writing good, correct, elegant
programs is to write them as if you wrote them for a
human reader, not a computer. Let’s clean up our
program.
CSE 1061 26
AST
U
“””
This program lets the robot go around his world counter-
clockwise, stopping when he comes back to his starting point.
“””
#Turn right.
def turn_right():
for i in range(3):
hubo.turn_left()
#Mark the starting point and move
def mark_and_move():
hubo.drop_beeper()
while not hubo.front_is_clear():
hubo.turn_left()
hubo.move()
(continued)
CSE 1061 27
AST
U
CSE 1061 28
AST
U
#Begin actual move.
mark_and_move()
#Follow the entire wall.
while not hubo.on_beeper():
follow_wall()
Hubo.turn_left()
CSE 1061 29
STEPWISE REFINEMENT AST
U
1. Start with a primitive program that solves a simple
problem.
2. Make small changes, one at a time to generalize the
program.
3. Make sure that each change does not invalidate
what you have done before.
4. Add appropriate comments (not just repeating what
the instruction does).
5. Choose descriptive names.
CSE 1061 30