Chamber Pressure

Source: Dec 2024, Task 3 (DTU 02002/02003). Study solution — not an official answer key.

Problem

Each hour, pressure increases by k * (Pmax - P). Start from P0; count hours until pressure reaches or exceeds Pcrit.

Write chamber_pressure(P0, Pmax, Pcrit, k) returning the number of hours (int).

Example
>>> chamber_pressure(20.0, 120.0, 105.0, 0.1)
19

Approach

Loop: update P = P + k * (Pmax - P), increment hours, stop when P >= Pcrit.

Solution

Show solution Hide solution
Solution
def chamber_pressure(P0, Pmax, Pcrit, k):
    P = P0
    hours = 0
    while P < Pcrit:
        P = P + k * (Pmax - P)
        hours += 1
    return hours