Have you ever received a phone call from an unrecognised number and wondered who was calling and where is the person calling you located? Did you know that, in the UK, for landline phone numbers (not mobile phones), the first 5 digits of the number are called the area code and gives you in indication of where this phone number originates from.
For instance any phone number starting with 01624 comes from the Isle of Man, whereas a number starting with 01224 comes from Aberdeen.
The aim of this challenge is to write a program that lets the user input a UK based landline phone number. The program will then perform a linear search on a large CSV file containing all of the UK based area codes and their matching locations. This linear search will enable the program to identify the exact location of the given phone number.
By completing this challenge you will further improve your file manipulation techniques.
If you cannot remember how to open and read through a text file or CSV check our blog post about “how to access text files using Python”.
Your challenge consists of writing a program that will:
Prompt the end-user to enter their telephone number.
Extract the area code (e.g. first 5 digits) for this number
Perform a linear search for this area code using the CSV provided file.
If the location is found, display the location this phone number is linked to.
If the location is not found, display a message to let the user know the location cannot be identified.
Here is the CSV file our programs will be based on:
Complete the Code
Once your code is done, complete the following tests to check that your code is working as it should:
If the area code cannot be found in the CSV file it may be because some locations, such as Manchester, only use the first 4 digits (e.g. 0161) as their area code. Adapt your code, so that if an area code cannot be found, the program performs a second linear search, but this time only looking for the first 4 digits of the phone number.
The CSV file also gives you the longitude and latitude coordinates of each area code. These coordinates can be used to calculate the distance between two sets of coordinates on the earth, using the Haversine formula. You could adapt your script to calculate the distance in miles between two UK phone numbers.
To find out more about the Haversine formula you may want to complete this challenge first: UK Postcodes distance calculator.