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.

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

ReadMe

For Pattern B, the program prints a sequence of numbers from 1 to n on the first line, 2 to n on the second line, etc., The starting number in the sequence increases by 1 on each subsequent line, while the ending number remains the same.

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. It involves rows of increasing numbers, where each number is repeated a number of times equal to its value.

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

Pattern N

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