3. Patterns – 2

“We can solve a complex problem
by breaking it apart and putting together
the solutions to the pieces.”

Theory

Libraries

A library is a collection of functions that we can load in our program and use as if we had written them. The editor of Karel has two tabs: “Python code” and “Library”. The “Library” tab shows us the contents of a library called my_lib.

Libraries contain functions that are useful for many programs. In this level we will find the following functions in the my_lib library:

In Python, we load all the functions of a library importing it as follows:

The star after the word import is called an asterisk and it means ‘everything’. In U.S. keyboards we can type it using shift-8 (i.e., press the shift key and then press the number 8). Leave a space between the word import and the asterisk.

More patterns

So far we have seen patterns with templates that are always oriented in the same direction.

Here we will look at patterns with templates that start the pattern in different directions.

Training Missions

2.TM1 Strawberries – Symmetry

Work it out

Karel is at home admiring his garden. Having strawberries only on the north stalls hurts his sense of symmetry. Please, help Karel move half of the plants in the north stalls to the south stalls and then take him back home and have him face the garden again.

Strawberry rows - Image from WashingtonFarms.net

Strawberry rows – Image from WashingtonFarms.net

Answer: Show it to me

Before...

Before…

..and after

..and after

Hint:

Use the library! To import the functions of the library into your program use

This mission is about describing the pattern and then going home. The main function could be something like:

2.TM2 Olympic dreams – Mountain Training

Work it out

Karel has been promised a spot on the downhill team if he is able to show that his legs have the strength necessary to cut it. The coach has signed us up for mountain training. Karel needs to climb the mountain, pick up some dumbbells on the way down, leave them at the base of the mountain, and then go home. It goes without saying that Karel cannot fly; he has to go up and down the steps.

Jenny Hadfield - Image from runnersworld.com

Jenny Hadfield – Image from runnersworld.com

Answer: Show it to me

Before...

Before…


...and after.

…and after.

Hint:

Use the library! To import the functions of the library into your program use

This mission is about describing the pattern and then going home. The main function could be something like:

In-class Mission Instructions

Hello again, Youngling. You and your droid are half way through the most important part of your training: how to break programs into functions. Hence, you need to keep in mind the rules to write good functions:

DO

…have your functions do only one thing and one thing only
…keep your functions short; the best functions are a few lines long
…give your functions a meaningful name so that anyone can understand what they do

and DO NOT

…have your functions do multiple things
…write page-long functions
…give your functions names that only make sense to you

The rules of how to solve a problem and get credit for it still stand:

  • We have solved a mission when we can write the solution from scratch without looking at the answer
  • A solution must be 30 lines of code or shorter.
  • To graduate from the level, submit solutions to all the 3 in-class missions at any time.

The use of the library requires adding two more rules:

  • Do not modify the library in any way.
  • Code in the library does not count towards the max. of 30 lines.

Keep up the good work, Youngling. You are almost there.

2.2 Strawberries – Strawberry fields

★★ Work it out

Wow! Karel has been busy. Look at all the strawberries in his strawberry patch. Harvest all the plants and take Karel home.

Feel free to use the functions from the library.

"Strawberry harvest at Louisiana State Exhibit Museum in Shreveport IMG 3360" by Billy Hathorn - Own work. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia Commons

“Strawberry harvest at Louisiana State Exhibit Museum in Shreveport IMG 3360” by Billy Hathorn – Own work. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia Commons

Answer: Show it to me

Before..

Before..

..and after.

..and after.

Hint:

We can split the collection of the 6 rows into three sets of two, with each of the sets consisting of a run going up followed by a run going down, as shown on the right.

Another possibility is to have 6 identical runs.

Collecting berries in both directions

Collecting berries in both directions

2.3 Life in SoCal – Softball coach

★★★ Work it out

Karel is volunteering to coach the local Pee-Wee softball team. He has to arrive to the field before anyone else and paint the lines between the bases that form the diamond, i.e., he has to put a token in each of the cells in the diagonal between the bases. Karel arrives to the field with 12 tokens in his bag. Help him paint the lines and then take him home.

L2-softball

Answer: Show it to me

Before...

Before…

...and after.

…and after.

Hint:

This problem is similar to the speed skating mission. The difference is that the square is inclined 45°..

Pattern for softball coach

Pattern for softball coach

Optional Mission – Does not give a star

2.4 Olympic dreams – Hockey me this

★★★★ Work it out

This had never happened!… The hockey final between the U.S.A. and Russia was stopped after 12 overtimes. The olympic committee debated and arrived to a decision: one way or another the game has to be decided tonight. To avoid a scandalous win via a coin toss, each team will select a player to demonstrate his skill by performing an extremely complicated move; the team whose player completes the maneuver will be the winner of the gold medal.

The chosen player has to skate a lap around the rink following a set of cones. There is no need to collect the cones.

"Ice Hockey sharks ducks" by Elliot - http://www.flickr.com/photos/pointnshoot/1426010816/. Licensed under Creative Commons Attribution 2.0 via Wikimedia Commons

“Ice Hockey sharks ducks” by Elliot – http://www.flickr.com/photos/pointnshoot/1426010816/. Licensed under Creative Commons Attribution 2.0 via Wikimedia Commons

The U.S.A. team has chosen Karel. Will he pull it off? Have Karel complete the figure and then enter the home square to receive the standing ovation.

Answer: Show it to me

Before...

Before…

...and after.

…and after.

Hint:

The following image shows a way to split the problem. The first half of the lap is identical to the second half and, each half follows an A-B-A pattern:

Possible way to divide the problem in parts

Possible way to divide the problem in parts

1.4 Olympic dreams – Speed skating

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

★★★★ Work it out

You cannot imagine the problem of fitting Karel with skates.. oh well. Karel is heading to the speed skating track: a single clove-shaped lap. Pick up all the 16 flags and then drop them at the podium. Will it be a medal or a contusion?

This is your chance to medal. Good luck.

"Paulien van Deutekom (08-12-2007)" by McSmit - Own work. Licensed under Creative Commons Attribution-Share Alike 3.0-2.5-2.0-1.0 via Wikimedia Commons

“Paulien van Deutekom (08-12-2007)” by McSmit – Own work. Licensed under Creative Commons Attribution-Share Alike 3.0-2.5-2.0-1.0 via Wikimedia Commons

Answer: Show it to me

Before...

Before…

..and after

..and after