11 Checkpoint: Functional Programming
Purpose
Reinforce in your mind the concept that a function can be passed as an argument into another function.
Problem Statement
Phone numbers in the U.S. are often stored as ten digits and two dashes in this format: "ddd-ddd-dddd" where each d is a digit. The first three digits are called the area code. For example, for many years, 208 was the area code for all phone numbers in the state of Idaho and 801 and 435 were the two area codes for phone numbers in the state of Utah. Some people, when entering their phone number, will not enter the area code and assume that other people will know the correct area code.
Assignment
Write a program named add_area_code.py
that reads
phone numbers from a text file and adds the area code "208-" to all
phone numbers that don’t have an area code.
Helpful Documentation
- The Reading Files article explains how to setup VS Code so that your Python program can read from a text file.
- The preparation content for lesson 9 explains how to read the contents of a text file into a list.
- The preparation content for this lesson explains higher-order functions, nested functions, lambda functions, and how to call the
map
function.
Steps
Do the following:
- Download these two files: phone_numbers.txt and add_area_code.py and save them in the same folder.
- Open the
phone_numbers.txt
file in VS Code and examine the phone numbers. Notice that some of the phone numbers do not have an area code and contain only seven digits. - Open the
add_area_code.py
file in VS Code. Read the comments in the program and then add the following code:- At line 12, write a call to the Python
map
function and pass theadd_area_code
function and phone_numbers list as arguments to themap
function. - At line 32, write code that checks if the length of a phone number is less than 12 and if the length is less than 12 then adds the area code "208-" at the beginning of the phone number.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Download, examine, and run this Python file named
test_add_area_code.py
that containspytest
test functions. Verify that all the test functions pass. If they don’t pass, fix the mistakes in youradd_area_code
function until they pass. - Run your program, which should print the list of corrected phone numbers as shown below. Examine the corrected phone numbers printed by your program and verify that all of them have an area code.
> python add_area_code.py ['208-318-1623', '208-384-7712', '986-314-1378', '307-377-4921', '208-671-7536', '986-228-7414', '208-693-6706', '208-274-2098', '208-854-3221', '208-361-9817', '208-422-3492', '208-631-1374', '986-533-9667', '208-158-9497', '208-892-9830', '208-477-5323', '406-204-8756', '208-805-2538', '208-913-9942', '208-216-9589', '406-138-9226', '801-644-6761', '208-814-4444', '208-822-6738', '208-892-1093', '208-936-6389', '435-656-5821', '435-605-8294', '208-271-1868', '208-894-6391', '208-232-4118', '208-132-2121', '208-515-8227', '208-848-2334', '208-426-1557', '208-952-4330', '208-998-2171', '208-743-9219', '208-944-4381', '208-589-7767', '208-407-3555', '208-928-9293', '208-957-5808', '208-940-6629', '208-825-9259', '208-123-7580', '208-374-8128', '208-847-4807', '986-159-8293', '208-515-9963', '406-241-6676', '208-907-1851', '208-297-6367', '208-936-8173', '208-376-7010', '208-681-1558', '208-800-4436', '208-590-8866', '208-788-1866', '208-163-7631', '208-928-8793', '406-595-2898', '208-330-3155', '208-884-7294', '208-120-4949', '801-343-8776', '208-362-3779', '208-482-2107', '208-366-5618', '435-957-8250', '208-441-5628', '307-494-6001', '208-571-5297', '208-366-7389', '801-730-8010', '208-134-6240', '208-402-3658', '208-151-9828', '208-485-7973', '208-506-2683', '208-518-9635', '208-769-7551', '208-776-1574', '208-604-4304', '208-119-9734', '208-917-6174', '208-773-1167', '307-749-5991', '208-334-8971', '208-995-8602', '208-193-5073', '208-345-4015', '208-367-8633', '208-289-6515', '208-265-8998', '208-800-3565', '208-211-2706', '208-408-3950', '208-928-3119', '208-691-9409']
Ponder
In the main
function, look at the line of code that
calls the map
function. Think about the fact that the
map
function will call the add_area_code
function once for each element in the phone_numbers list,
and the add_area_code
function will add the area code
"208-" at the beginning of each phone number if necessary.
Sample Solution
When your program is finished, view the sample solution for this assignment to compare your solution to that one. Before looking at the sample solution, you should work to complete this checkpoint program. However, if you have worked on it for at least an hour and are still having problems, feel free to use the sample solution to help you finish your program.
Submission
When complete, report your progress in the associated I‑Learn quiz.