In some parts of the world, phone numbers are fixed-length, apparently. That sounds boring, though.
For this challenge, your task is to take the digits of a what is presumed to be a valid phone number, and format it based on some simplified rules around what a phone number looks like.
Input format
Your input will consist of nothing except the digits 0
through 9
, and the input will contain between five and ten of them (both inclusive). Every input will be a valid phone number, according to the following simplified rules:
- A full phone number consists of eight to ten digits and can be divided into an optional area code of two to four digits (the first of which is
0
), and a mandatory subscriber number of five to eight digits (the first of which is not0
) - When calling someone from a landline phone who has the same area code as you, you do not need to dial the area code. Thus, your program must be able to handle inputs consisting only of a subscriber number with no area code.
- The subscriber number consists of 5 to 8 digits, and the first one isn't
0
. - The area code, if present, begins with
0
, and has 2 to 4 digits. For this challenge's purposes, the length depends on the second digit:- The second digit of the area code will never be
0
. - If the second digit is
8
, the entire area code will be 2 digits long (so the entire area code will be08
) - If the second digit is
1
,2
,3
,4
, or7
, the area code will be 3 digits long. - If the second digit is
5
,6
, or9
, the area code will be 4 digits long.
- The second digit of the area code will never be
- However, for some inputs, the above cutoff point would cause the subscriber number to be too short or begin with a
0
. Since we know the input is a valid phone number (because I say so) we have to conclude that the length of the area code is different from the above. If the length listed above does not work, you should pick a length which works, preferring 3-digit codes over 4-digit codes, and preferring either of those to 2-digit codes. You will not need to handle inputs like0801234
, which are invalid for all those cutoff points.
Output format
Your output should be the area code, followed by a dash (only if an area code is present), followed by the subscriber number which should be grouped in one of the following manners (with the groups separated by a single space):
xxx xxx xx
xxx xx xx
xx xx xx
xxx xx
That means numbers with area codes specified will have one of the following formats:
0x-xxx xxx xx
0x-xxx xx xx
0x-xx xx xx
0xx-xxx xx xx
0xx-xx xx xx
0xx-xxx xx
0xxx-xx xx xx
0xxx-xxx xx
Test cases
All numbers were randomly generated, and some of them could potentially be in use, so while I don't expect any of them to belong to anyone in particular, please don't try to call them.
27983516 -> 279 835 16 # No area code, 8 digits
6278304 -> 627 83 04 # No area code, 7 digits
627830 -> 62 78 30 # No area code, 6 digits
11865 -> 118 65 # No area code, 5 digits
067934821 -> 0679-348 21 # Simple 4+5
0927586130 -> 0927-58 61 30 # Simple 4+6
0374821096 -> 037-482 10 96 # Simple 3+7
042807961 -> 042-80 79 61 # Simple 3+6
04629873 -> 046-298 73 # Simple 3+5
0897106384 -> 08-971 063 84 # Simple 2+8
081859302 -> 08-185 93 02 # Simple 2+7
08379451 -> 08-37 94 51 # Simple 2+6
0480936572 -> 0480-93 65 72 # Area code couldn't be 048 to prevent subscriber number starting with 0
0957024168 -> 095-702 41 68 # Area code couldn't be 0957 for the same reason
06057291 -> 060-572 91 # Area code couldn't be 0605 because of subscriber number's length
04809714 -> 04-80 97 14 # Area code could neither be 048 nor 0480 didn't help
080062517 -> 0800-625 17 # Area code could neither be 08 nor 080
09106574 -> 09-10 65 74 # Area code had to be shortened twice from 0910 because shortening it to 091 didn't work
This is code-golf, shortest code in each language wins.
0574821096 -> 057-482 10 96
? Why not-> 0574-82 10 96
? \$\endgroup\$0574-82 10 96
would be the correct output for that test case, thanks for pointing it out. \$\endgroup\$