Heater click counter
EasyOnline Assessment
Replay a log of room temperatures through a thermostat and count how often the heater clicks on.
The thermostat has a target temperature and a deadband band so the heater does not flutter on and off. It reads the temperature once per minute and decides, in this order:
- If the heater is on and the reading is at or above
target + band, the heater switches off. - If the heater is off and the reading is at or below
target - band, the heater switches on, unless it is still resting (see below). - Otherwise nothing changes. A reading strictly between
target - bandandtarget + bandnever changes the state.
To protect the compressor there is a rest period: after the heater switches off, the next rest readings cannot switch it on, whatever they say. Those readings are used up even if they are not cold enough to matter. The reading that switches the heater off is not part of the rest. With rest = 0 there is no rest period.
The heater is off before the first reading, and the first reading is free to switch it on.
Input:
readings: the temperature at each minute, in whole degrees.target: the target temperature.band: the deadband, at least 1.rest: the number of readings the heater must rest after switching off.
Output: a list [switch_ons, minutes_on]: how many times the heater switched on, and how many minutes it was on after the thermostat handled that minute's reading (a minute on which the heater switches on counts as on; a minute on which it switches off counts as off).
Example 1
- Input:
- [[17,19,21,22,19,18,23],20,2,0]
- Output:
- [2,4]
- Explanation
- Target 20 and band 2: the heater switches on at 18 or below and off at 22 or above. Reading 17 switches it on, 19 and 21 leave it on, 22 switches it off, 19 is not cold enough to restart it, 18 switches it on again, and 23 switches it off. That is 2 switch-ons. The heater was on after the readings 17, 19, 21 and 18, so 4 minutes:
[2, 4].
Example 2
- Input:
- [[17,21,24,15,14,13,24],20,2,2]
- Output:
- [2,3]
- Explanation
- Rest is 2. The heater switches on at 17, stays on at 21, and switches off at 24. The next two readings (15 and 14) are cold enough but fall inside the rest period, so they are ignored. The reading 13 switches it on again, and 24 switches it off. Two switch-ons, on after the readings 17, 21 and 13:
[2, 3].
Example 3
- Input:
- [[18,20,22,18,22,19,18],20,2,0]
- Output:
- [3,4]
- Explanation
- With no rest, both boundaries are inclusive: 18 switches on, 22 switches off, then 18 on, 22 off, 19 changes nothing (off), and 18 switches on again. Three switch-ons. The heater counts as on after the readings 18, 20, 18 and 18, so the answer is
[3, 4].
Constraints
0 <= len(readings) <= 400-50 <= readings[i] <= 60-20 <= target <= 401 <= band <= 100 <= rest <= 10