Skip to content

Commit 83e52f5

Browse files
committed
feat: add pattern printing (pyramid, inverted pyramid, diamond) [#13460]
1 parent 788d95b commit 83e52f5

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

other/pyramid_patterns.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
patterns/pyramid_patterns.py
3+
4+
Pattern Printing Examples:
5+
- Pyramid
6+
- Inverted Pyramid
7+
- Diamond
8+
9+
Demonstrates loops, functions, and conditionals for beginners.
10+
11+
Example:
12+
>>> pyramid(3)
13+
*
14+
***
15+
*****
16+
17+
>>> inverted_pyramid(3)
18+
*****
19+
***
20+
*
21+
22+
>>> diamond(3)
23+
*
24+
***
25+
*****
26+
***
27+
*
28+
"""
29+
30+
# No import needed for None type annotation
31+
32+
def pyramid(n: int) -> None:
33+
"""Prints a pyramid pattern of height n."""
34+
for i in range(n):
35+
print(" " * (n - i - 1) + "*" * (2 * i + 1))
36+
37+
38+
def inverted_pyramid(n: int) -> None:
39+
"""Prints an inverted pyramid pattern of height n."""
40+
for i in range(n - 1, -1, -1):
41+
print(" " * (n - i - 1) + "*" * (2 * i + 1))
42+
43+
44+
def diamond(n: int) -> None:
45+
"""Prints a diamond pattern of height n."""
46+
pyramid(n)
47+
for i in range(n - 2, -1, -1):
48+
print(" " * (n - i - 1) + "*" * (2 * i + 1))
49+
50+
51+
if __name__ == "__main__":
52+
print("Pyramid:")
53+
pyramid(5)
54+
print("\nInverted Pyramid:")
55+
inverted_pyramid(5)
56+
print("\nDiamond:")
57+
diamond(5)

0 commit comments

Comments
 (0)