The pyramid of glasses
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- decimals
- loops
- lists
Problem
Dmitri, the waiter at the wedding, stacks glasses into a pyramid: one on top (row 0), two below it and so on; row r has r + 1 glasses, numbered from the left starting at 0, and there are always rows to spare.
He pours punch glassfuls of punch on top (a whole number, 0 or more). Each glass holds 1, and anything over 1 spills half and half: glass pos in a row pours into glasses pos and pos + 1 of the row below. A glass that is not full spills nothing.
The couple will toast with glass pos of row row (0 <= pos <= row). Return how full it gets, as a decimal from 0 to 1: 0 if nothing reaches it and 1 if it ends up full, even if more falls into it.
With 4 glassfuls poured, the top glass keeps 1 and spills 3: each glass in row 1 gets 1.5, keeps 1 and spills 0.5. In row 2, the middle one gets 0.25 from each side and ends at 0.5, and the ones on the edges get 0.25 each. With row 2 and pos 1 you return 0.5.
Examples
The example
4, 2, 1 → 0.5
A glass on the edge
4, 2, 0 → 0.25
Half and half
2, 1, 0 → 0.5
The top one is never more than full
5, 0, 0 → 1
No punch yet
0, 0, 0 → 0
A glass filled exactly spills nothing
1, 1, 1 → 0
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def fill_level(punch, row, pos):
passJavaScript
function fillLevel(punch, row, pos) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.