Given an integer n, generate two patterns of height n:
-
A pyramid pattern. Each row
i
(1-indexed) contains
2*i - 1
asterisks (
*
) centered within a total width of
2*n - 1
characters, with spaces padding both sides.
-
A trapezium pattern. Each row
i
(1-indexed) contains
n + i - 1
asterisks left-aligned within a total width of
2*n - 1
characters, with spaces padding the right side.
Return a list containing two lists of strings: the first list is the pyramid pattern and the second is the trapezium pattern. Each string is one row of the pattern, from top to bottom, and has a fixed width of 2*n - 1 characters.
Function Signature
def generate_patterns(n: int) -> list[list[str]]:
Rules
-
Rows are returned as strings, not printed.
-
Every string must be exactly
2*n - 1
characters long, trailing spaces included. Do not strip them.
-
The only characters allowed are
*
and the space character.
Constraints
Examples
Input: n = 4
Output: [[" * "," *** "," ***** ","*******"],["**** ","***** ","****** ","*******"]]
The width is 7. Drawn out, the pyramid rows are * , *** , ***** and ******* (1, 3, 5 and 7 stars centered), and the trapezium rows are **** , ***** , ****** and ******* (4 to 7 stars, left-aligned).
Input: n = 1
Output: [["*"],["*"]]
With n = 1 the width is 2*1 - 1 = 1, so both patterns are a single row containing one asterisk.
Input: n = 2
Output: [[" * ","***"],["** ","***"]]
The width is 3. The pyramid has 1 star with one space on each side, then 3 stars. The trapezium has 2 stars and one trailing space, then 3 stars.