Int 3 for Programmers

Which readings are in range

Which readings are in range

A sensor logs whole-number readings all day. A technician wants only the ones inside a given interval, and the interval may include or exclude either endpoint, exactly as the brackets in interval notation do.

Write in_range(low, high, low_closed, high_closed, values), which returns the readings that lie in the interval, in the order they were logged. low_closed being true means a square bracket on the left, so a reading equal to low is kept; false means a round bracket, so it is dropped. high_closed does the same job on the right.

Worked example, with low = -3, high = 5, low_closed = true, high_closed = false and values = [-4, -3, 0, 5, 6]. That is the interval [−3,5)[-3, 5). The reading -4 is below the left end. The reading -3 is kept because the left end is closed. The reading 0 is inside. The reading 5 is dropped because the right end is open, and 6 is outside. The answer is [-3, 0].

  • Constraints: values has at most 100 entries and may be empty. Each reading is between -1000 and 1000.
  • low is never greater than high, but they may be equal.
  • Keep the original order, and keep repeats: two readings of the same value both belong in the answer.
  • An empty answer is a correct answer when nothing qualifies.
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.