Skip to content

Commit ee6bbbe

Browse files
authored
Add function to calculate pyramid height from blocks
This function calculates the height of a pyramid based on the number of blocks available, raising an error for negative inputs.
1 parent 7e3840b commit ee6bbbe

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Week03/pyramid_furkan_bulut.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def calculate_pyramid_height(number_of_blocks):
2+
"""
3+
Calculate the height of a pyramid that can be built with a given number of blocks.
4+
5+
The function determines how many complete layers (height) can be formed
6+
using the provided number of blocks. Each layer requires a number of blocks
7+
equal to the layer number (1 block for the first layer, 2 blocks for the
8+
second layer, etc.).
9+
10+
:param number_of_blocks: The total number of blocks available to build the pyramid.
11+
:type number_of_blocks: int
12+
:return: The maximum height of the pyramid that can be built.
13+
:rtype: int
14+
15+
:raises ValueError: If `number_of_blocks` is less than 0.
16+
17+
Example:
18+
19+
calculate_pyramid_height(6)
20+
3
21+
calculate_pyramid_height(20)
22+
5
23+
"""
24+
if number_of_blocks < 0:
25+
raise ValueError("Number of blocks must be non-negative.")
26+
27+
height_of_pyramid = 0
28+
block_counter = 0
29+
30+
while number_of_blocks >= (block_counter + (height_of_pyramid + 1)):
31+
block_counter += height_of_pyramid + 1
32+
height_of_pyramid += 1
33+
34+
return height_of_pyramid

0 commit comments

Comments
 (0)