14. Advanced Looping

../../_images/nested_loops.png

Games involve a lot of complex loops. This is a “challenge chapter” to learn how to be an expert with loops. If you can understand the problems in this chapter, by the end of it you can certify yourself as a loop expert.

At the very least, make sure you can write out the answers for the first eight problems. That will give you enough knowledge to continue this book.

You can try solving these on your computer, or on-line with repl.it. Scroll down to the “05” set of problems.

14.2. Advanced Looping Problems

14.2.1. Problem 1

Write code that will print ten asterisks (*) in a row:

* * * * * * * * * *

Write code to print the asterisks using a for loop. Print each asterisk individually, don’t just print all ten with one “print” statement. This problem can be completed in two lines of code using one for loop and one print statement.

Remember, don’t look at the answer until you’ve figured it out yourself, or you have been trying for 5-10 minutes. (I don’t recommend waiting longer than ten minutes.)

Answer to Problem 1

14.2.2. Problem 2

Write code that will print the following:

* * * * * * * * * *
* * * * *
* * * * * * * * * * * * * * * * * * * *

This is just like the prior problem, but also printing five and twenty stars. Copy and paste from the prior problem, adjusting the for loop as needed. Don’t forget how Moving to the Next Line.

Answer to Problem 2

14.2.3. Problem 3

Use two “for” loops, one of them nested inside the other, to print the following 10x10 rectangle:

* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *

To start, take a look at Problem 1. The code in Problem 1 generates one line of asterisks. It needs to be repeated ten times. You’ll also need to move to the next line after each row has printed. Work on this problem for at least ten minutes before looking at the answer.

Answer to Problem 3

14.2.4. Problem 4

Use two “for” loops, one of them nested, to print the following 5x10 rectangle:

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

This is a lot like the prior problem. Experiment with the ranges on the loops to find exactly what the numbers passed to the range function control.

Answer to Problem 4

14.2.5. Problem 5

Use two for loops, one of them nested, to print the following 20x5 rectangle:

* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *

Again, like Problem 3 and Problem 4, but with different range values.

Answer to Problem 5

14.2.6. Problem 6

Write code that will print the following:

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

Use two nested loops. Print the first line with a loop. Don’t use code like this:

print("0 1 2 3 4 5 6 7 8 9")

Attention

First, create a loop that prints the first line. Then enclose it in another loop that repeats the line 10 times. Use either i or j variables for what the program prints. This example and the next one helps reinforce what those index variables are doing.

Work on this problem for at least ten minutes before looking at the answer. The process of spending ten minutes working on the answer is far more important than the answer itself.

Answer to Problem 6

14.2.7. Problem 7

Adjust the prior program to print:

0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9

Answer to Problem 7

14.2.8. Problem 8

Write code that will print the following:

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

Tip: This is just problem 6, but the inside loop no longer loops a fixed number of times. Don’t use range(10), but adjust that range amount.

Answer to Problem 8

Make sure you can write out the code for this and the prior problems. Repeat until you can get it without looking up the answer. Yes, this practice is work, but it will pay off later and you’ll save time in the long run.

14.2.9. Problem 9

Write code that will print the following:

0 1 2 3 4 5 6 7 8 9
  0 1 2 3 4 5 6 7 8
    0 1 2 3 4 5 6 7
      0 1 2 3 4 5 6
        0 1 2 3 4 5
          0 1 2 3 4
            0 1 2 3
              0 1 2
                0 1
                  0

This one is difficult. Tip: Two loops are needed inside the outer loop that controls each row. First, a loop prints spaces, then a loop prints the numbers. Loop both these for each row. To start with, try writing just one inside loop that prints:

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

Then once that is working, add a loop after the outside loop starts and before the already existing inside loop. Use this new loop to print enough spaces to right justify the other loops.

Answer to Problem 9

14.2.10. Problem 10

Write code that will print the following (Getting the alignment is hard, at least get the numbers):

1   2   3   4   5   6   7   8   9
2   4   6   8  10  12  14  16  18
3   6   9  12  15  18  21  24  27
4   8  12  16  20  24  28  32  36
5  10  15  20  25  30  35  40  45
6  12  18  24  30  36  42  48  54
7  14  21  28  35  42  49  56  63
8  16  24  32  40  48  56  64  72
9  18  27  36  45  54  63  72  81

Tip: Start by adjusting the code in problem 1 to print:

0  0  0  0  0  0  0  0  0  0
0  1  2  3  4  5  6  7  8  9
0  2  4  6  8  10  12  14  16  18
0  3  6  9  12  15  18  21  24  27
0  4  8  12  16  20  24  28  32  36
0  5  10  15  20  25  30  35  40  45
0  6  12  18  24  30  36  42  48  54
0  7  14  21  28  35  42  49  56  63
0  8  16  24  32  40  48  56  64  72
0  9  18  27  36  45  54  63  72  81

Then adjust the code to print:

1  2  3  4  5  6  7  8  9
2  4  6  8  10  12  14  16  18
3  6  9  12  15  18  21  24  27
4  8  12  16  20  24  28  32  36
5  10  15  20  25  30  35  40  45
6  12  18  24  30  36  42  48  54
7  14  21  28  35  42  49  56  63
8  16  24  32  40  48  56  64  72
9  18  27  36  45  54  63  72  81

Finally, use an 11if`` to print spaces if the number being printed is less than 10. (Or use string formatting if you are already familar with that.)

Answer to Problem 10

14.2.11. Problem 11

Write code that will print the following:

                1
              1 2 1
            1 2 3 2 1
          1 2 3 4 3 2 1
        1 2 3 4 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

Tip: first write code to print:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9

Then write code to print:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

Then finish by adding spaces to print the final answer.

Answer to Problem 11

14.2.12. Problem 12

Write code that will print the following:

                1
              1 2 1
            1 2 3 2 1
          1 2 3 4 3 2 1
        1 2 3 4 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7
      1 2 3 4 5 6
        1 2 3 4 5
          1 2 3 4
            1 2 3
              1 2
                1

This can be done by combining problems 11 and 9.

Answer to Problem 12

14.2.13. Problem 13

Write code that will print the following:

                1
              1 2 1
            1 2 3 2 1
          1 2 3 4 3 2 1
        1 2 3 4 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
        1 2 3 4 5 4 3 2 1
          1 2 3 4 3 2 1
            1 2 3 2 1
              1 2 1
                1

Answer to Problem 13