Merge remote-tracking branch 'origin/main'

main
Thomas 2023-01-13 19:03:26 +01:00
commit cc2db74e4b
2 changed files with 49 additions and 0 deletions

16
SL_2/s2_a4_a.py 100644
View File

@ -0,0 +1,16 @@
import re
def normalize_number(phone_number):
pattern = re.compile("([\+]*[1][ -])*(\(*[2-9][0-9][0-9]\)*)[- .]([0-9][0-9][0-9])[ -.]([2-9][0-8][0-9][0-9])")
if not pattern.match(phone_number):
raise ValueError("Ungültige Telefonnummer")
normalized_number = re.sub("\+*\(*\)*", "", phone_number)
normalized_number = re.sub("[ .]", "-", normalized_number)
if normalized_number[0] != str(1):
normalized_number = "1-" + normalized_number
return normalized_number

33
SL_2/s2_a4_b.py 100644
View File

@ -0,0 +1,33 @@
import unittest
from s2_a4_a import normalize_number
class TestPhoneNumber(unittest.TestCase):
test_correct_numbers = [
"+1 223-456-7890",
"1-223-456-7890",
"+1 223 456-7890",
"(223) 456-7890",
"1 223 456 7890",
"223.456.7890",
"1-989-111-2222"
]
test_wrong_numbers = [
"+1 123-234-3333",
"1 222-333-1111",
"222-333-9999",
"22-333-4444"
]
def test_given_numbers_correct(self):
for numbers in self.test_correct_numbers[:-1]:
self.assertEqual("1-223-456-7890", normalize_number(numbers))
self.assertEqual("1-989-111-2222", normalize_number("1-989-111-2222"))
def test_number_exception(self):
for numbers in self.test_wrong_numbers:
with self.assertRaises(ValueError) as context:
normalize_number(numbers)
self.assertTrue('This is broken' in context.exception)