镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Closed
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(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)