Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions other/pyramid_patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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(n: int) -> None:
"""Prints a pyramid pattern of height n."""
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))


def inverted_pyramid(n: int) -> None:
"""Prints an inverted pyramid pattern of height n."""
for i in range(n - 1, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))


def diamond(n: int) -> None:
"""Prints a diamond pattern of height n."""
pyramid(n)
for i in range(n - 2, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))


if __name__ == "__main__":
print("Pyramid:")
pyramid(5)
print("\nInverted Pyramid:")
inverted_pyramid(5)
print("\nDiamond:")
diamond(5)