DAY 04 OF 100 · Types · validation

Easy

A Sensor Feed You Can Trust.

Today’s challenge

Build the solution

Write a function called clean_temperatures(values) that validates a mixed sensor feed.

Return contract: A dictionary with readings (a list of integers) and rejected (a list of zero-based indexes).

Input bounds: 0–200 JSON values; each string is at most 80 characters. Only ASCII 0–9 count as digits.

Keep the supplied driver below your implementation. It reads one JSON object and prints your return value as JSON. The driver may also probe the behaviour described above; those probe fields are part of the test input.

  1. Accept integer values and strings containing an ASCII integer, with optional leading + or - and optional surrounding whitespace.
  2. Accept only temperatures from -40 through 85 inclusive. Reject booleans, floats, empty strings, a sign without digits, and other values.
  3. Preserve the order of accepted readings. Report the zero-based positions of rejected values.

The Twist: In Python, True behaves like the integer 1 in many expressions. It is still a faulty reading for this contract.

Sample

Input

{"values":[25," -40 ","85",true,25.0,"86","bad"]}

Expected output

{"readings":[25,-40,85],"rejected":[3,4,5,6]}
day-4.py
Python 3
Tab inserts four spaces. Press Escape, then Tab to move to the next control, or Shift+Tab to move to the previous control.

Log in or create an account to run code, submit solutions, and save your progress.

Output

Run your code to see the result here.