3. Patterns – 1

“functions are the lego-blocks that we use to build programs”

Theory

Patterns

A pattern is something that repeats with regularity. For example, a horse has a galloping pattern:

A template is the sequence of steps that form a pattern when we repeat them: the beginning and the end of the template has to be the same for the pattern to work out:

Patterns occur often in programming. Our goals in this level are:

  1. recognizing the existence of a pattern
  2. identifying the template, i.e., the places where the pattern repeats itself
  3. programming the pattern with a function

Training Missions

3.TM Strawberries – Single file

First study the answer, then solve the problem on your own

Work it out

In this mission we will use functions to describe a pattern.

Karel wants to plant 10 strawberry plants in each of the 7 cells in front of his house. Karel starts with 70 plants in his bag. Help Karel be the best farmer he can be and then take him home.

Before: Ready to plant the strawberries

Before: Ready to plant the strawberries


After: All done

After: All done

Answer: Show it to me

Our main function has to turn Karel to the right, then plant all the strawberries and then go home. The following program solves the mission completely:

# main function
right()
plant_berries()
go_home()

Now let’s work out the secondary functions. There is no command to turn right but we can effectively turn right if we turn left three times. To make the program more readable, we’ll write an alias to turn_left() and use it to write our right() function:

def left():
    turn_left()

def right():
    repeat( left, 3 )

There are two ways in which we can plant the strawberries. In the first one, we spell out what Karel needs to do; in the second one, we look for the pattern, write a function that describes it, and then use the function. Clearly, the second approach is better than the first:

Version 1:

def pick_up_berries():
    move()
    repeat( put, 10 )
    move()
    repeat( put, 10 )
    move()
    repeat( put, 10 )
    move()
    repeat( put, 10 )
    move()
    repeat( put, 10 )
    move()
    repeat( put, 10 )
    move()
    repeat( put, 10 )

Version 2:

def move_and_put():
    move()
    repeat( put, 10 )

def plant_berries():
    repeat( move_and_put, 7 )

After picking up the berries Karel will be in the cell to the left of the home square. Hence, go_home() is simply an alias of move(). The complete final program is:

def left():
    turn_left()

def right():
    repeat( left, 3 )

def move_and_put():
    move()
    repeat( put, 10 )

def plant_berries():
    repeat( move_and_put, 7 )

def go_home():
    move()

# main function
right()
plant_berries()
go_home()

In-class Missions

3.1 Strawberries – The diagonal garden

Try to solve the problem w/o peeking at the answer

Work it out

Pick up the strawberries and then take Karel home.

Strawberry, ''gariguette'' variety Copyright © 2005 David Monniaux

Strawberry, ”gariguette” variety Copyright © 2005 David Monniaux

Answer: Show it to me


Ready to pick up the strawberries

Ready to pick up the strawberries

all done!

all done!


Hint:

Use functions to split the mission in independent parts. The main function could be something like this:

# main program
go_to_berries()
repeat( take_berry, 5 )
go_home()

3.2 Strawberries – Fertilizer

Try to solve the problem w/o peeking at the answer

★★ Work it out

Well.. the fertilizer worked. Karel just got an order of 8 pallets of 10 bags each, all of which have to be stored in their corresponding sheds. Nasty work but if we want strawberry shortcake we need the strawberries so….

Image from vegetable-garden-basics.com

Image from vegetable-garden-basics.com

Answer: Show it to me

Before...

Before…

..and after

..and after

Hint:

Use a function to describe the pattern.

3.3 Olympic dreams – Obstacle course

Try to solve the problem w/o peeking at the answer

★★★ Work it out

This year Karel has a chance to join the Winter Olympic team. To improve his timing and balance, he set up an obstacle course in front of his house. We need to write the following routines:

sidestep_left()     # move to cell on left, face in original dir
sidestep_right()    # move to cell on right, face in original dir

or, graphically:

sidestep_left()

and

sidestep_right

Use these functions to take Karel home.

Answer: Show it to me

Before...

Before…

..and after.

..and after.

Hint:

Use a function to describe the pattern. The main function could be something like this:

# main program
repeat (up_and_down, 5)

Optional Mission

3.4 Olympic dreams – Downhill

Try to solve the problem w/o peeking at the answer

★★★★ Work it out

Sweet! (Karel doing happy dance here). Karel is on the downhill team. Take Karel down the hill, weaving between the flags, to the trophy presentation room (or, depending on our programming skills, to the first-aid station).

"Andrej Šporn at the 2010 Winter Olympic downhill" by Jon Wick - originally posted to Flickr as Go Time. Licensed under Creative Commons Attribution 2.0 via Wikimedia Commons

“Andrej Šporn at the 2010 Winter Olympic downhill” by Jon Wick – originally posted to Flickr as Go Time. Licensed under Creative Commons Attribution 2.0 via Wikimedia Commons

Answer: Show it to me


Before...

Before…


...and after.

…and after.