-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathsplit-number.py
More file actions
41 lines (30 loc) · 974 Bytes
/
split-number.py
File metadata and controls
41 lines (30 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
"""
Splits phone numbers into their country code, local area code, and number.
https://www.hackerrank.com/challenges/split-number
"""
import re
def split_phone_numbers(numbers):
"""Prints the separated groups of phone numbers."""
pattern = get_pattern()
for number in numbers:
match = pattern.match(number)
output = ('CountryCode={country_code},'
'LocalAreaCode={area_code},'
'Number={number}').format(**match.groupdict())
print(output)
def get_pattern():
"""Compiles a regex pattern that groups phone number parts."""
regex = (
'(?P<country_code>\d{1,3})'
'[ -]'
'(?P<area_code>\d{1,3})'
'[ -]'
'(?P<number>\d{4,10})'
)
pattern = re.compile(regex)
return pattern
if __name__ == '__main__':
number_count = int(input())
numbers = (input() for _ in range(number_count))
split_phone_numbers(numbers)