Skip to content

Instantly share code, notes, and snippets.

@AlvisonHunterArnuero
Created October 29, 2025 06:00
Show Gist options
  • Select an option

  • Save AlvisonHunterArnuero/5ef91a72cece7fe37b263455f39219fd to your computer and use it in GitHub Desktop.

Select an option

Save AlvisonHunterArnuero/5ef91a72cece7fe37b263455f39219fd to your computer and use it in GitHub Desktop.
def print_rows(rows: int, i: int, char: str) -> None:
"""Print a single row of the diamond pattern.
Args:
rows: Total number of rows in one half of the diamond
i: Current row number
char: Character to use for the diamond pattern
"""
print(" " * (rows - i) + char * (2 * i - 1))
def print_diamond(rows: int, char: str = "*") -> None:
"""Print a diamond pattern with the specified number of rows and character.
Args:
rows: Number of rows in one half of the diamond
char: Character to use for the diamond pattern (default: "*")
Raises:
ValueError: If rows is less than 1
"""
if rows < 1:
raise ValueError("Rows must be at least 1")
# Print top half (increasing)
for i in range(1, rows + 1):
print_rows(rows, i, char)
# Print bottom half (decreasing)
for i in range(rows - 1, 0, -1):
print_rows(rows, i, char)
def print_diamond_centered(rows: int, char: str = "*", padding: str = " ") -> None:
"""Print a diamond pattern with centered alignment.
Args:
rows: Number of rows in one half of the diamond
char: Character to use for the diamond pattern (default: "*")
padding: Character to use for padding (default: " ")
Raises:
ValueError: If rows is less than 1
"""
if rows < 1:
raise ValueError("Rows must be at least 1")
max_width = 2 * rows - 1
# Print top half
for i in range(1, rows + 1):
stars = char * (2 * i - 1)
padding_needed = (max_width - len(stars)) // 2
print(padding * padding_needed + stars)
# Print bottom half
for i in range(rows - 1, 0, -1):
stars = char * (2 * i - 1)
padding_needed = (max_width - len(stars)) // 2
print(padding * padding_needed + stars)
def get_diamond_string(rows: int, char: str = "*") -> str:
"""Return the diamond pattern as a string instead of printing it.
Args:
rows: Number of rows in one half of the diamond
char: Character to use for the diamond pattern (default: "*")
Returns:
String representation of the diamond pattern
Raises:
ValueError: If rows is less than 1
"""
if rows < 1:
raise ValueError("Rows must be at least 1")
lines = []
# Generate top half
for i in range(1, rows + 1):
lines.append(" " * (rows - i) + char * (2 * i - 1))
# Generate bottom half
for i in range(rows - 1, 0, -1):
lines.append(" " * (rows - i) + char * (2 * i - 1))
return "\n".join(lines)
def print_hollow_diamond(rows: int, char: str = "*") -> None:
"""Print a hollow diamond pattern (only outline).
Args:
rows: Number of rows in one half of the diamond
char: Character to use for the diamond pattern (default: "*")
Raises:
ValueError: If rows is less than 1
"""
if rows < 1:
raise ValueError("Rows must be at least 1")
# Print top half
for i in range(1, rows + 1):
if i == 1:
# First row: single character
print(" " * (rows - i) + char)
else:
# Middle rows: characters at edges only
spaces_between = 2 * i - 3
print(" " * (rows - i) + char + " " * spaces_between + char)
# Print bottom half
for i in range(rows - 1, 0, -1):
if i == 1:
# Last row: single character
print(" " * (rows - i) + char)
else:
# Middle rows: characters at edges only
spaces_between = 2 * i - 3
print(" " * (rows - i) + char + " " * spaces_between + char)
# Demonstration of all functions
if __name__ == "__main__":
print("=== Original Diamond ===")
print_diamond(5)
print("\n=== Diamond with Custom Character ===")
print_diamond(4, "#")
print("\n=== Centered Diamond ===")
print_diamond_centered(5)
print("\n=== Hollow Diamond ===")
print_hollow_diamond(5)
print("\n=== Diamond as String ===")
diamond_str = get_diamond_string(3, "+")
print(diamond_str)
print("\n=== Error Handling Example ===")
try:
print_diamond(0)
except ValueError as e:
print(f"Error: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment