Patterns, Patterns Everywhere

patternB.py: 2 points
patternC.py: 2 points
patternN.py: 3 points


In the next batch of problems, we’re going to be looking at sequences of patterns. Your task is to first determine the rules that describe how a particular pattern in the sequence relates to the corresponding index. Then you’ll write a program that generates a chosen pattern from the given sequence.

For each pattern, you’ll have the following input and output:

  • Input: A positive integer n that represents an index in the pattern.
  • Output: The n-th figure of the pattern.

You have already done pattern A in the warmup. Now let’s do three more. (Remember to commit and push your code after you complete each pattern.)

Pattern B

Enter a positive integer: 3

1 2 3
2 3
3 

Enter a positive integer: 4

1 2 3 4
2 3 4
3 4
4

Enter a positive integer: 5

1 2 3 4 5
2 3 4 5
3 4 5 
4 5
5

Pattern C

Enter a positive integer: 3

1 
1 2 2 
1 2 2 3 3 3

Enter a positive integer: 4

1 
1 2 2 
1 2 2 3 3 3
1 2 2 3 3 3 4 4 4 4

Enter a positive integer: 5

1 
1 2 2 
1 2 2 3 3 3
1 2 2 3 3 3 4 4 4 4
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

ReadMe

Pattern C can be generated with three loops: (1) one for each row, (2) one for which number you’re on in the row, and (3) one to generate the repeated number.

Pattern N

Enter a positive integer: 2

*  *
** *
* **
*  *

Enter a positive integer: 3

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

Enter a positive integer: 4

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

ReadMe

For Pattern N, it will help to break the patterns into multiple parts. Notice that the top and bottom row are the same, whereas the middle rows have their own pattern.

Testing

Don’t forget to test your programs on a variety of inputs. Your programs will be graded using additional inputs (e.g., n = 6 or n = 10).