Editor

Check the Sign-Up Records

#609 · Errors and exceptions · Intermediate · 80 XP · about 16 min

A sign-up form sends each person as a dictionary of text, such as {'name': ' Asha ', 'age': '15', 'email': 'Asha@School.org'}. Write check_records(records) that returns a tuple (good, problems):

  • good is a list of the clean records, in order. Each is a new dictionary with the keys 'name', 'age' and 'email': the name without spaces around it, the age as an int, and the email without spaces around it and in lower case.
  • problems is a list of messages, one for each record that is not good, such as 'record 2: age is not a number'. Records are numbered from 1.

Check each record in this order, and report only its first problem:

  • A missing key: 'record 3: missing age'. Check name, then age, then email.
  • A name that is empty once the spaces are removed: 'record 3: name is empty'.
  • An age that int() cannot read: 'record 3: age is not a number'.
  • An age below 13 or above 120: 'record 3: age out of range'.
  • An email that does not contain exactly one @: 'record 3: bad email'.

Example

check_records([
    {'name': ' Asha ', 'age': '15', 'email': 'Asha@School.org'},
    {'name': 'Ben', 'age': 'ten', 'email': 'ben@mail.com'},
    {'name': 'Cara', 'email': 'cara@mail.com'},
])
# returns ([{'name': 'Asha', 'age': 15, 'email': 'asha@school.org'}],
#          ['record 2: age is not a number', 'record 3: missing age'])

Constraints

  • At most 10,000 records, and every value is a string.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.