beginner

Case File: The Messy Contact List

Before anybody can send a text message, the contact list has to be cleaned: names in capitals, in lower case and with extra spaces, and phone numbers typed in a dozen styles. Clean the names with split and join, reduce every phone number to its digits, accept the two valid Nigerian mobile forms, and mark the rest invalid. Learn string methods, a regular expression for digits, and why a rule for validity must be explicit. The contacts are generated for you alone.

45 min

Sabi Logistics Β· Python 8

The Messy Contact List

Last week a delivery text message went to a number with one digit missing, and a stranger replied angrily

Emeka Support engineer Β· Phone call

Emeka. We text customers when their parcel is out for delivery. The numbers in our list were typed in by different people: with spaces, dashes, brackets, a plus sign, a 234 in front, a zero in front. Last week a message went to a number with one digit missing, and somebody else received it and was not pleased.

Please clean the list. Names with proper capitals and single spaces. Phone numbers in one standard form (the 11 digits that start with 0), and when a number cannot be a valid mobile number, say so (None) instead of guessing, and tell me how many were invalid.

The brief

What you need to know first.

  • Names. text.split() with nothing in the brackets splits on any amount of white space and drops empty pieces, so " ada OKAFOR " gives ["ada", "OKAFOR"]. word.capitalize() makes the first letter a capital and the rest lower case. " ".join(words) glues the words with single spaces. An empty name stays an empty text.
  • Digits only. re.sub(r"[^0-9]", "", text) (after import re) replaces everything that is not a digit by nothing, so "+234 803-123 4567" becomes "2348031234567". This handles spaces, dashes, brackets and the plus sign in one line.
  • The rule for a valid number. A Nigerian mobile number is valid in two forms: 11 digits that start with 0 (08031234567), or 13 digits that start with 234 (2348031234567), which is rewritten as 0 plus the last 10 digits. Anything else (10 digits, 12 digits, empty) is invalid: return None. Writing the rule down is the point: the program never guesses.
  • Counting the invalid ones. n_invalid is the number of contacts whose cleaned phone is None. Keep the contact in the list even when its phone is invalid, so that a person can fix it.
  • Fill in clean_name, clean_phone, clean_all(req) returning {"cleaned", "n_invalid"}, then cleaned and n_invalid. The checks run clean_all on lists that you cannot see: mC dONALD, an empty contact, a number one digit short, a 234 number, and an empty list.

The lab gives you the data for this case, invented and generated for you alone.

Preparing your data…

πŸ”’ The debrief unlocks when you close the case. Finish the lab above.

Write the contact cleaning note

Write the note for the support engineer: how names are cleaned (split, capitalize, join) and why the method is too simple for names such as McDonald, how phone numbers are reduced to digits and which two forms are valid, how many contacts had an invalid number out of the total, why the program marks them invalid and does not guess, and what to do next (keep the name as typed, a phone library, validation at the door, no messages to failed numbers).

0 words

πŸ§ͺ Want to practise this on your own computer? See how to set up your lab →

πŸ€–

Need clarification? Ask GT

Hi, I'm GT β€” your AI tutor for Case File: The Messy Contact List. What would you like to know?
πŸ“

Knowledge Check

1 of 3

What does " ada OKAFOR ".split() give?

βš‘ Capture The Flag

Report a problem

About: /lessons/case-the-messy-names