Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cyantarek/192b8e2b15fa7bdfd9f129f6f851831a to your computer and use it in GitHub Desktop.
Save cyantarek/192b8e2b15fa7bdfd9f129f6f851831a to your computer and use it in GitHub Desktop.
import sys, re
if sys.version_info.major != 3:
raise ValueError("You must use Python 3.")
if sys.version_info.minor < 4:
raise ValueError("You must use at least Python 3.4")
if sys.version_info.minor < 6:
print("Recommended Python Version is 3.6")
test_phone_numbers = [
"+49174321324",
"0164883423",
"0049(0)16483311724",
"+49-8332-3010",
"00498513994",
"0164 5554454",
"0851 509",
"0851 509-0",
"(0851) 3394"
]
gold_phone_numbers = [
"+49 174 321324",
"+49 164 83311724",
"+49 83323010",
"+49 8513994",
"+49 164 883423",
"+49 164 5554454",
"+49 851509",
"+49 8515090",
"+49 8513394"
]
def validate_phone(parsed_numbers):
for parsed, test, gold in zip(parsed_numbers, test_phone_numbers, gold_phone_numbers):
if parsed != gold:
print("WRONG ? \n\t Input : {}\n\t Gold : {}\n\t Parsed: {}".format(test, gold, parsed))
else:
print("CORRECT ? \n\t Input : {}\n\t Gold : {}\n\t Parsed: {}".format(test, gold, parsed))
parsed_list = []
cleaned_list = []
telephone_list = []
non_telephone_list = []
country_coded_list = []
non_country_coded_list = []
valid_list = []
for i in test_phone_numbers:
a = i.replace("(0)", "")
a = a.replace("-", "")
a = a.replace(" ", "")
a = a.replace("(", "")
a = a.replace(")", "")
cleaned_list.append(a)
for i in cleaned_list:
try:
country_check1 = re.match(r"^[+]49$", i[:3])
except:
country_check1 = None
if not country_check1:
try:
country_check2 = re.match(r"^0+49", i[:5])
except:
country_check2 = None
if country_check2:
country_coded_list.append(i.replace("0049", "+49 "))
else:
non_country_coded_list.append(i)
else:
country_coded_list.append(i.replace("+49", "+49 "))
for i in country_coded_list:
try:
zero_check = re.match(r"^[0]$", i[4])
except:
zero_check = None
if not zero_check:
valid_list.append(i)
for i in valid_list:
try:
one_check = re.match(r"^[1]$", i[4])
except:
one_check = None
if one_check:
telephone_list.append(i)
else:
non_telephone_list.append(i)
for i in telephone_list:
tel_code = i[4:7]
total = "+49 " + tel_code + " " + i[7:]
parsed_list.append(total)
for i in non_telephone_list:
total = "+49 " + i[4:]
parsed_list.append(total)
for i in non_country_coded_list:
if len(i) < 10:
a = "+49 " + i[1:]
parsed_list.append(a)
else:
a = "+49 " + i[1:4] + " " + i[4:]
parsed_list.append(a)
validate_phone(parsed_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment