for
SolutionsJed Rembold
January 21, 2026
yDzKvf
import karel
def main():
"""Refoliage 3 trees"""
for i in range(3):
find_next_tree()
refoliage_tree()
def find_next_tree():
"""Moves Karel to the base of a tree
Algorithm: move forward until tree in face
"""
while front_is_clear():
move()
def refoliage_tree():
"""Add leavese back to the top of the tree
Algorithm: climb tree, add leaves, descend tree
"""
climb_tree()
add_leaves()
descend_tree()
def climb_tree():
"""Climbs one tree to the top
Algorithm: reorient to face upwards, climb until no tree to
my right
"""
turn_left() # face upwards
while right_is_blocked():
move()
def add_leaves():
"""Adds leaves to the top of a tree
Algorithm: move up to top of leaves, and then snake way
across top and back to end up in bottom right
"""
for i in range(2): #Already one above the top of the tree
move()
turn_right()
add_leaf_row() #top
turn_right() # Make first corner
move()
turn_right()
add_leaf_row() #middle
turn_left() # Second corner
move()
turn_left()
add_leaf_row() #bottom
def add_leaf_row():
"""Adds a row of 3 leaves
Algorithm: Place one beeper first to avoid fencepost problem,
then loop twice
"""
put_beeper() # Beepers across bottom
for i in range(2):
move()
put_beeper()
def descend_tree():
"""Positions and moves back down the tree to the ground
Algorithm: orient downwards, move until group, and then
orient to east
"""
while not_facing_south():
turn_left()
while front_is_clear():
move()
turn_left()
def turn_right():
"""Turns Karel to the right"""
for i in range(3):
turn_left()