Dial it right: phone number normaliser
A call-centre CRM collects phone numbers from many countries, typed in every style imaginable. Before dialling, each number must be converted to the international form +<country code><national number> or rejected.
Known countries (country code: length of the national number): 1: 10, 44: 10, 91: 10, 92: 10, 971: 9. A national number never starts with 0.
- First remove spaces, dashes, dots and round brackets. Any other character makes the number
INVALID(the only allowed+is the first character). - If the cleaned text starts with
+, the digits after it must be a known country code followed by a national number of the right length. - If it starts with
00, treat the00as a+and continue as above. - If it starts with a single
0(national style, the character after it is not a second zero), drop that0and put the default country code in front. The rest must be a valid national number for that country. - Anything else is
INVALID.
Task: line 1 is the default country code (one of the known codes). Line 2 is N. Then N lines with raw numbers. For each, print the normalised number (+ followed by country code and national digits, no separators) or INVALID.
Sample tests
Input
92 5 0300-1234567 +92 (300) 1234567 0092 300 1234567 0300-123456 +44 20 7946 0958
Expected output
+923001234567 +923001234567 +923001234567 INVALID +442079460958
Input
44 3 020 7946 0958 +971 50 123 4567 (0)20 7946 0958
Expected output
+442079460958 +971501234567 +442079460958
Your answer is also checked against 3 hidden tests.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
