File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments