Better a good phone neighbour than a distant friend. With that idea in mind, the editorial officers of the digital experiment Sambal1 attempted to spark some new friendships around Flanders in December 2014. To test the new concept, each Sambal team member sent the following text message to his or her phone neighbours, hoping for a quick response.
But what exactly are your phone neighbours? Well, simple enough, they are the people who have almost the same phone number as yourself, but then one number higher or lower. For example, if your phone number is 0472/91.39.17, then your downstairs neighbour has phone number 0472/91.39.16 and your upstairs neighbour has phone number 0472/91.39.18.
Getting to know your phone neighbours is a two-step process. At first, off course, you send them a text message. If you receive an answer, you take a screenshot of the conversation that follows. The best and funniest screenshots are collected on a temporary Facebook page2.
In this assignment, we represent a phone number as a string that contains both digits and other characters. Each phone number contains at least one digit. The other characters are used to format the phone number in some way or another. As an example, the phone number 0472/91.39.17 is formatted using one extra slash and three extra dots. The goal of this assignment is to determine the phone neighbours of a given phone number, and express them in the same format as the original phone number. This is done in the following way:
Write a function digits that takes a phone number as its argument. The function must return a string that only contains the digits of the given phone number, in the order in which they appear in the given phone number.
Write a function replace that takes two arguments: a phone number and a positive integer (an int). The function must replace each of the digits of the given phone number by the digits of the given integer and return the result. In case the given phone number contains more digits than the given integer, the extra digits in front of the phone number must be replaced by zeros. In case the given phone number contains less digits than the given integer, the excess digits at the end of the integer should not be used in the substition.
Use the functions digits and replace to write a function upstairsNeighbour that takes a phone number as its argument. The function must return the upstairs neighbour of the given phone number.
Use the functions digits and replace to write a function downstairsNeighbour that takes a phone number as its argument. The function must return the downstairs neighbour of the given phone number. The function may assume that not all digits of the given phone number are zeros.
>>> digits('0472/91.39.17')
'0472913917'
>>> digits('++32 (0)9 264 4779')
'32092644779'
>>> replace('0472/91.39.17', 1234567890)
'1234/56.78.90'
>>> replace('++32 (0)9 264 4779', 123456789)
'++00 (1)2 345 6789'
>>> upstairsNeighbour('0472/91.39.17')
'0472/91.39.18'
>>> upstairsNeighbour('++32 (0)9 264 4779')
'++32 (0)9 264 4780'
>>> downstairsNeighbour('0472/91.39.17')
'0472/91.39.16'
>>> downstairsNeighbour('++32 (0)9 264 4779')
'++32 (0)9 264 4778'