## 11. 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!

### 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.

### Challenges

We end this lesson with a few exercises. Respecting the theme of this lesson (but not the title!), the first two exercises are repetitions of two exercises we had at the end of the lesson on building walls. 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.

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

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.

Your program should define the following instructions:

    move_to_first_row()
    harvest_two_rows()
    move_right_to_next_row()

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

    def harvest_two_rows():
        harvest_one_row()
        move_left_to_next_row()
        harvest_one_row()

with an approppriate definition for `harvest_one_row()`. However, you could choose your own way of breaking up the should required instructions. With the three required instructions defined, my version of the program, other than the definitions, is written (with still a few repetitions) as follows:

    move_to_first_row()
    harvest_two_rows()
    move_right_to_next_row()
    harvest_two_rows()
    move_right_to_next_row()
    harvest_two_rows()
    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.

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

    move_to_first_row()
    harvest_two_rows()
    move_right_to_next_row()

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()
    harvest_two_rows()
    move_right_to_next_row()
    harvest_two_rows()
    move_right_to_next_row()
    harvest_two_rows()
    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.

Definitely avoiding repetitions - - If only Reeborg could decide on his own ...