An e-bike's motor controller is tested on a roller: the rider twists the throttle fully open at sample 0, and the bike's speed (km/h) is logged every dt seconds. How lively does it feel? Engineers measure the rise time: how long the speed takes to go from 10 % to 90 % of the way from its starting value to its final value. The very start (the motor overcoming friction) and the slow creep at the end are left out, because they depend on details nobody notices when riding.
With start = samples[0] and the given final speed (final > start),
- the 10 % level is
start + 0.1 * (final - start)and the 90 % level isstart + 0.9 * (final - start); i10is the first sample index whose value is at or above the 10 % level,i90the first at or above the 90 % level;- the rise time is
(i90 - i10) * dtseconds.
Write rise_time(samples, dt, final) that returns the rise time, or None if the log never reaches the 90 % level. Press Run with plot(samples) to see the test run.
Examples
Input: samples = [0, 1, 3.5, 9, 15, 20, 23.2, 24.5, 25], dt = 0.5, final = 25
Output: 2.0
Explanation: the levels are 2.5 and 22.5 km/h. Sample 2 (3.5) is the first at or above 2.5,
sample 6 (23.2) the first at or above 22.5: (6 - 2) * 0.5 = 2.0 s.
Input: samples = [5, 5.2, 6, 7, 7.5], dt = 0.1, final = 8
Output: None
Explanation: the 90 % level is 7.7 km/h and the log stops at 7.5.
Constraints
- answers are compared with a tolerance of
1e-6; do not round
Goals
- Turn percentages of a step into levels between its start and its final value
- Find the first sample at or above a level
- Measure rise time from 10 % to 90 % and explain why the ends are left out