Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gmolveau
GitHub Repository: gmolveau/python_full_course
Path: blob/master/exercices/imperative/pyramid/solution_pyramid.py
306 views
1
def print_pyramid(height: int):
2
"""in the following example for height = 4, we see that
3
* > 3 spaces + 1 stone (+ 3 spaces optional)
4
*** > 2 spaces + 3 stones + (2 spaces optional)
5
***** > 1 space + 5 stones + (1 space optional)
6
******* > 0 space + 7 stones
7
with current_height starting at 0
8
we add 2 'stones' for each new line, so 2 * current_height + 1
9
those stones have (height - current_height - 1) * spaces on the left
10
"""
11
for current_height in range(height):
12
stones = "*" * (2 * current_height + 1)
13
space = " " * (height - current_height - 1)
14
print(f"{space}{stones}{space}")
15
16
17
height = 4
18
print_pyramid(height)
19
20