Skip to content

Instantly share code, notes, and snippets.

@RafaelBroseghini
Created December 15, 2018 18:03
Show Gist options
  • Save RafaelBroseghini/187c802904af39522f5d59d69826afda to your computer and use it in GitHub Desktop.
Save RafaelBroseghini/187c802904af39522f5d59d69826afda to your computer and use it in GitHub Desktop.
Draw Asteriks Pyramids in any direction
"""
This short script allows to draw a asteriks pyramid
in any direction desired. 'Left' is the default direction.
A 'middle' pyramid has n steps with (n*2-1) asteriks.
"""
def draw_pyramid(steps: int, direction: str = "left") -> None:
possible_directions = set(["left", "right", "middle"])
if direction not in possible_directions:
raise ValueError(f"'{direction}' Direction not supported.")
if direction == "left":
for i in range(1, steps+1):
print(i*"*", (steps-i)*" ")
elif direction == "right":
for i in range(1, steps+1):
print((steps-i)*" ",i*"*")
else:
steps *= 2
for i in range(1, steps+1, 2):
print(((steps-i)//2+1)*" ", i*"*", ((steps-i)//2+1)*" ")
if __name__ == "__main__":
draw_pyramid(10)
draw_pyramid(10, "right")
draw_pyramid(10, "middle")
draw_pyramid(10, "center")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment