Calvin (Deutschbein)
W2D2: 4 Sep
I believe on Mac you can simply doubleclick.
Does it happen more than once? | |||
No | Yes (it's a loop) | ||
Is it conditional (do we ask Karel a question)? |
No |
|
|
Yes |
|
|
import karel
def main():
move()
move()
move()
move()
move()
move()
move()
move()
move()
turn_left() # can only turn left
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left() # can only turn left
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left() # can only turn left
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
import karel
def main():
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left() |
import karel
def main():
move_nine()
turn_left()
move_nine()
turn_left()
move_nine()
turn_left()
move_nine()
turn_left()
def move_nine():
move() # 1
move() # 2
move() # 3
move() # 4
move() # 5
move() # 6
move() # 7
move() # 8
move() # 9
|
Which is easier?
|
import karel
def main():
move_nine() # call
turn_left()
move_nine() # call
turn_left()
move_nine() # call
turn_left()
move_nine() # call
turn_left()
def move_nine(): # define
move() # 1
move() # 2
move() # 3
move() # 4
move() # 5
move() # 6
move() # 7
move() # 8
move() # 9
|
Which is easier?
import karel
def main():
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left() |
import karel
def main():
for i in range(9):
move()
turn_left()
for i in range(9):
move()
turn_left()
for i in range(9):
move()
turn_left()
for i in range(9):
move()
turn_left()
|
Which is easier?
import karel
def main():
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left() |
import karel
def main():
move_nine()
turn_left()
move_nine()
turn_left()
move_nine()
turn_left()
move_nine()
turn_left()
def move_nine():
for i in range(9):
move()
|
Which is easier?
import karel
def main():
move_nine()
turn_left()
move_nine()
turn_left()
move_nine()
turn_left()
move_nine()
turn_left()
def move_nine():
for i in range(9):
move() |
import karel
def main():
for i in range(4):
move_nine()
turn_left()
def move_nine():
for i in range(9):
move() |
Which is easier?
import karel
def main():
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left()
move() # move to end
move()
move()
move()
move()
move()
move()
move()
move()
turn_left() |
import karel
def main():
for i in range(4):
move_nine()
turn_left()
def move_nine():
for i in range(9):
move()
|
What if this were 2x2?
(I was talking to my elders about home-ownership this weekend 😭)
|
Not necessarily easier, but more general.
On the homework, you can't drop a beeper (you have none) - I'm not doing the problem for you.
import karel
def main():
put_beeper()
cling_left_until_beeper()
pick_and_rev()
cling_right_until_beeper()
pick_beeper()
def cling_left_until_beeper():
move()
def pick_and_rev():
move()
def cling_right_until_beeper():
move() |
import karel
def main():
put_beeper()
cling_left_until_beeper()
pick_and_rev()
cling_right_until_beeper()
pick_beeper()
def cling_left_until_beeper():
# this will hit walls
while no_beepers_present():
move()
def pick_and_rev():
move()
def cling_right_until_beeper():
move() |
import karel
def main():
put_beeper()
cling_left_until_beeper()
pick_and_rev()
cling_right_until_beeper()
pick_beeper()
def cling_left_until_beeper():
while no_beepers_present():
if front_is_blocked():
# avoid walls
turn_right()
# or 3 left
move()
def pick_and_rev():
move()
def cling_right_until_beeper():
move() |
import karel
def main():
put_beeper()
cling_left_until_beeper()
pick_and_rev()
cling_right_until_beeper()
pick_beeper()
def cling_left_until_beeper():
while no_beepers_present():
if front_is_blocked():
# avoid walls
turn_right()
# or 3 left
if left_is_clear():
# 'cling' left
turn_left()
move()
def pick_and_rev():
move()
def cling_right_until_beeper():
move() |
def main():
while no_beepers_present():
if front_is_blocked():
# avoid walls
turn_left()
turn_left()
turn_left()
# or 3 left
if left_is_clear():
# 'cling' left
turn_left()
move()