PY-21

Validate a Small Record

  • Easy–Medium
  • Records and Validation
  • Python

Task

Write validate_record(record). A record is a dictionary with three required keys: id, value, and split. The function returns (valid, reason), where valid is a Boolean and reason explains the result.

Check the requirements in this exact order:

  1. Check for the required keys in the order id, value, split. If one is missing, return (False, "missing <key>") immediately.
  2. An id must not be an empty string. Otherwise return (False, "empty id").
  3. value must be between 0 and 100, including both boundaries. Otherwise return (False, "value out of range").
  4. split must be either "train" or "test". Otherwise return (False, "unknown split").

If all checks pass, return (True, "ok"). Inputs use the stated field types, and the record must not be changed.

Example

The second result reports the empty identifier because it is the first failing rule; later fields are not allowed to change that reason.

Your implementation

Edit solution.py and keep this function name and signature:

Return exactly a two-item tuple containing a Boolean and one of the stated reason strings. Do not print, ask for input, or mutate record.