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.
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)(afterimport 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 as0plus the last 10 digits. Anything else (10 digits, 12 digits, empty) is invalid: returnNone. Writing the rule down is the point: the program never guesses. - Counting the invalid ones.
n_invalidis the number of contacts whose cleaned phone isNone. 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"}, thencleanedandn_invalid. The checks runclean_allon 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β¦
Emeka: βYour cleaning turned McDonald into Mcdonald. Somebody complained. Is capitalize wrong?β
π The debrief unlocks when you close the case. Finish the lab above.
The debrief
Emeka: βSo the simple rule is wrong for some real names.β
What an experienced person would do next. Know that names are hard: McDonald, OβNeil, van der Berg, and names in other scripts all break simple capital rules. The safest policy for a customer list is to keep the name as the customer typed it (only trim and collapse spaces) and apply fancy capitals only for display, if at all. For phone numbers, use a real library (such as phonenumbers) in production, which knows every country; here we wrote the rule ourselves to see how it works. Validate at the door: a form that checks the number when it is typed, and sends a code to it, prevents the missing digit in the first place. And never send a message to a number that failed the check.
The trap. Guessing: adding a digit, or taking the first 11 digits of a long number. A wrong guess sends a message to a stranger.
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).
π§ͺ Want to practise this on your own computer? See how to set up your lab →