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

ReadMe

A useful thing to remember here is the print() method’s end= capabilities. Review your lecture slides to remember how to use this.

If you are stuck at any point, revisit the warmup and follow the process outlined for pattern A to think through your approach to these patterns.

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.

If you approach this differently and end up using more/less than three loops, that is also completely fine.

Pattern N

Note: This one is a bit more challenging! If you’re struggling with it make sure you finish the last part of the lab and come back to it!

Enter a positive integer: 2

*  *
** *
* **
*  *

Enter a positive integer: 3

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

Enter a positive integer: 4

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

ReadMe

For Pattern N, you can divide the structure into distinct parts:

  1. The top row and bottom row are identical, with two stars separated by spaces.
  2. The middle rows form the diagonal pattern connecting the two stars.

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