Problem 165782 · medium · Level 01 Prerequisites & Setup

When Has the Oven Settled?

step response · settling time · tolerance band · scanning backwards

A bakery's oven is switched from 20 °C to 200 °C, and its controller brings it there with a few wobbles. The baker wants to know when the oven is ready: the settling time is the first moment after which the temperature stays within 2 % of the size of the step around the final value, for the rest of the log. Briefly passing through the band does not count if the temperature leaves it again later.

You get the log samples (one sample every dt seconds; samples[0] is at time 0, the moment of the command) and the final value. The band half-width is

band = 0.02 * abs(final - samples[0])

and a sample s is inside when abs(s - final) <= band. Return the time i * dt of the smallest index i such that samples[i] and every later sample are inside. If the last sample is outside, the oven has not settled within the log: return None.

The step can go up or down (a cooling oven works the same way). Press Run with plot(samples) to see the wobbles.

Examples

Input:  samples = [20, 120, 175, 196, 206, 203, 198, 199.5, 201, 200.2], final = 200, dt = 30
Output: 150
Explanation: the band is 0.02 * 180 = 3.6 °C, so inside means 196.4 to 203.6.
196 is just outside and 206 is outside; from 203 (index 5, at 150 s) onwards every sample is inside.

Input:  samples = [0, 5, 9, 10.5], final = 10, dt = 1
Output: None
Explanation: the band is 0.2 and the last sample is 0.5 away.

Constraints

  • final != samples[0]
  • answers are compared with a tolerance of 1e-6

Goals

  • Turn a percentage tolerance into a band around the final value
  • Find the moment after which a signal stays inside a band for good
  • Tell apart entering a band and staying in it
Starting Python…