hopscotch

Category: Programming
Author: Marty Stepp
Book Chapter: 4.2
Problem: hopscotch
Write a static method hopscotch that shows a sequence of numbers similar to the appearance of a hopscotch board. (Hopscotch is a "hopping" game played by children.) For this problem, a hopscotch board is an increasing sequence of numbers in lines in an alternating pattern. Odd lines contain a single number indented by 3 spaces. Even lines contain a pair of numbers with the first number not indented, followed by 5 spaces, followed by the second number.

Your method should accept a parameter representing the number of "hops" worth of numbers to display. A board of 0 "hops" shows only the number 1, indented by 3 spaces. A "hop" is defined as a trio of additional numbers spread across 2 additional lines, following the pattern described previously. So for example, 1 hop means to show the numbers 2-3-4 to the board in the pattern shown below. 2 hops means to show the numbers 2-3-4, 5-6-7 on the board. 3 hops means to show 2-3-4, 5-6-7, 8-9-10, and so on. Assume that the number of hops passed will be at least 0.

Call    | hopscotch(0); | hopscotch(1); | hopscotch(2); | hopscotch(3); | hopscotch(5);
-----------------------------------------------------------------------------------------
Output  |   1           |   1           |   1           |   1           |   1
        |               |2     3        |2     3        |2     3        |2     3
        |               |   4           |   4           |   4           |   4
        |               |               |5     6        |5     6        |5     6
        |               |               |   7           |   7           |   7
        |               |               |               |8     9        |8     9
        |               |               |               |   10          |   10
        |               |               |               |               |11     12
        |               |               |               |               |   13
        |               |               |               |               |14     15
        |               |               |               |               |   16