Avoiding repetitions, again!

Rule #3 that we saw in the previous lesson is so important that I feel I must repeat it so as to help you remember it well.

Rule # 3
When writing computer programs, do not repeat yourself.
I repeat: do not repeat yourself!

1. Repeat!

In your latest solution to the newspaper delivery exercise, there was probably still a fair bit of repetition. For example, the command turn_left() probably appeared three times in a row in the definition of turn_right(). Similarly, climb_up_one_stair() probably appeared four times in the definition of climb_up_four_stairs(). This seems to run contrary to our rule about not repeating ourselves. A way to avoid such repetition is to ask Reeborg to repeat instructions through a special command.

To have Reeborg repeat an instruction, we use the command repeat() as follows:
repeat( name of instruction, number of times)

Note that the name of instruction is the name without the parentheses () at the end. For example, we could write

def turn_right():
    repeat(turn_left, 3)

Your turn

Make use of repeat everywhere you can to write a shorter version of the newspaper delivery challenge. Make sure that your new program works as expected.


2. Challenges

We end this lesson with a few exercises. Respecting the theme of this lesson, the first two exercises are repetitions of two exercises we had at the end of the lesson on beepers. To solve them, you have to make use of the new concepts [def, repeat()] you have seen in this lesson and in the previous one.

Jumping hurdles

Reeborg has entered a hurdles race. Write a new program that have him follow the path indicated below in his way to the finish line. The world file is hurdles1.wld.

hurdles start
lead to hurdles end

It might be useful to define, among others, a new instruction, jump_hurdle(), which would correspond to the following path:

jump one hurdle start lead to jump one hurdle end

Compare your new solution with the previous one. [You did save it, didn't you?]


Harvest time

It's harvest time! Have Reeborg pick up all the carrots (represented by beepers) in the garden shown below. The world file is harvest1.wld.

harvest start

Your program should define the following instructions:

move_to_first_row()
harvest_two_rows()

You may want to break up these instructions further; for example, you could have:

def harvest_two_rows():
    harvest_one_row()
    move_to_next_row()
    harvest_one_row()
    move_to_next_row()

However, you could choose your own way of breaking up the two required instructions. With both required instructions defined, your program should be written as follows:

move_to_first_row()
repeat(harvest_two_rows, 3)
turn_off()

Again, compare your new solution with the previous one you had for the harvesting problem.

Harvest time again!

It's harvest time again! However, this time the rows in the garden have been set diagonally. Have Reeborg pick up all the carrots (represented by beepers) in the garden shown below. The world file is harvest4.wld.

harvest start

As in the previous example, your program should define the following instructions:

move_to_first_row()
harvest_two_rows()

These instructions will not be defined the same way as before. However, once you have these instructions defined, your program should be written in the same way as the previous example:

move_to_first_row()
repeat(harvest_two_rows, 3)
turn_off()

Careful not to hit a wall! Think of where you want to start harvesting, and in which direction you are going to harvest. It may help to sketch a path on a piece of paper.

previous Definitely avoiding repetitions - home - If only Reeborg could decide on his own ... next
../images/SourceForge.net Logo