Skip to content

Instantly share code, notes, and snippets.

@unacceptable
Created December 31, 2025 04:08
Show Gist options
  • Select an option

  • Save unacceptable/5bc04a509228afef70788e60c7855280 to your computer and use it in GitHub Desktop.

Select an option

Save unacceptable/5bc04a509228afef70788e60c7855280 to your computer and use it in GitHub Desktop.
'''
Elegant hourglass pattern generator.
'''
import itertools
def hourglass(n: int, char: str = "*") -> str:
'''
Generate an hourglass pattern.
Args:
n: Number of rows for the widest part
char: Character to use for the pattern
Returns:
The hourglass pattern as a string
Example (n=6):
* * * * * *
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
* * * * * *
'''
rows = itertools.chain(range(n, 0, -1), range(2, n + 1))
return "\n".join(f"{' ' * (n - i)}{f'{char} ' * i}".rstrip() for i in rows)
if __name__ == "__main__":
row = int(input("Enter the number of rows: "))
print(hourglass(row))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment