Created
December 31, 2025 04:08
-
-
Save unacceptable/5bc04a509228afef70788e60c7855280 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| 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