-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
Expand file tree
/
Copy pathpyramid_patterns.py
More file actions
58 lines (44 loc) · 1.08 KB
/
pyramid_patterns.py
File metadata and controls
58 lines (44 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
patterns/pyramid_patterns.py
Pattern Printing Examples:
- Pyramid
- Inverted Pyramid
- Diamond
Demonstrates loops, functions, and conditionals for beginners.
Example:
>>> pyramid(3)
*
***
*****
>>> inverted_pyramid(3)
*****
***
*
>>> diamond(3)
*
***
*****
***
*
"""
# No import needed for None type annotation
def pyramid(height: int) -> None:
"""Prints a pyramid pattern of the specified height."""
for i in range(height):
print(" " * (height - i - 1) + "*" * (2 * i + 1))
def inverted_pyramid(height: int) -> None:
"""Prints an inverted pyramid pattern of the specified height."""
for i in range(height - 1, -1, -1):
print(" " * (height - i - 1) + "*" * (2 * i + 1))
def diamond(height: int) -> None:
"""Prints a diamond pattern of the specified height."""
pyramid(height)
for i in range(height - 2, -1, -1):
print(" " * (height - i - 1) + "*" * (2 * i + 1))
if __name__ == "__main__":
print("Pyramid:")
pyramid(5)
print("\nInverted Pyramid:")
inverted_pyramid(5)
print("\nDiamond:")
diamond(5)