From 0b7b0ce07b6f82a31bc30328564e56c73d8a5939 Mon Sep 17 00:00:00 2001 From: Eren <2120548@stud.hs-mannheim.de> Date: Mon, 9 Jan 2023 17:41:29 +0100 Subject: [PATCH] Finished Task S2 for PR3 --- Personen.txt | 2 ++ PersonenNeu.json | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ s2_a1.py | 26 ++++++++++++++++++++ s2_a2.py | 10 ++++++++ s2_a3.py | 53 +++++++++++++++++++++++++++++++++++++++++ s2_a4_a.py | 21 ++++++++++++++++ s2_a4_b.py | 33 ++++++++++++++++++++++++++ 7 files changed, 207 insertions(+) create mode 100644 Personen.txt create mode 100644 PersonenNeu.json create mode 100644 s2_a1.py create mode 100644 s2_a2.py create mode 100644 s2_a3.py create mode 100644 s2_a4_a.py create mode 100644 s2_a4_b.py diff --git a/Personen.txt b/Personen.txt new file mode 100644 index 0000000..e44510c --- /dev/null +++ b/Personen.txt @@ -0,0 +1,2 @@ +Dr. Lucas Linus Kreutzer, 19.12.2022, Heidelberger Allee 18 68305 Mannheim, 0123456789 +Ing. Birgid Oestrovsky, 2022-03-12, Heidelberger Allee 18 68305 Mannheim, 0123456789 \ No newline at end of file diff --git a/PersonenNeu.json b/PersonenNeu.json new file mode 100644 index 0000000..044c066 --- /dev/null +++ b/PersonenNeu.json @@ -0,0 +1,62 @@ +[ + { + "Index": 1, + "Titel": [ + "Dr." + ], + "Vorname": [ + "Lucas" + ], + "Zweitname": [ + "Linus" + ], + "Nachname": [ + "Kreutzer" + ], + "Stra\u00dfe": [ + "Heidelberger Allee" + ], + "Hausnummer": [ + "18" + ], + "PLZ": [ + "68305" + ], + "Wohnort": [ + "Mannheim" + ], + "Geburtsdatum": [ + "19.12.2022" + ] + }, + { + "Index": 2, + "Titel": [ + "Ing." + ], + "Vorname": [ + "Birgid" + ], + "Zweitname": [ + null + ], + "Nachname": [ + "Oestrovsky" + ], + "Stra\u00dfe": [ + "Heidelberger Allee" + ], + "Hausnummer": [ + "18" + ], + "PLZ": [ + "68305" + ], + "Wohnort": [ + "Mannheim" + ], + "Geburtsdatum": [ + "12.03.2022" + ] + } +] \ No newline at end of file diff --git a/s2_a1.py b/s2_a1.py new file mode 100644 index 0000000..c149bc6 --- /dev/null +++ b/s2_a1.py @@ -0,0 +1,26 @@ +class X: + pass + + +class Y(X): + pass + + +class Z(X, Y): + pass + + +xyz = Z() + +# L[X] = [X] + merge([X]) +# L[X] = [X] + +# L[Y] = [Y] + merge(L(X), [Y]) +# L[Y] = [Y] + merge([X], [Y]) +# L[Y] = [Y, X] + merge([Y]) +# L[Y] = [Y, X] + +# L[Z] = [Z] + merge(L(X), L(Y), [X, Y]) +# L[Z] = [Z] + merge([X], [Y, X], [X, Y]) +# >> cannot pick X +# >> cannot pick Y diff --git a/s2_a2.py b/s2_a2.py new file mode 100644 index 0000000..ad287b0 --- /dev/null +++ b/s2_a2.py @@ -0,0 +1,10 @@ +import re + +text = "If the the problem is textual, use the the re module" + +# \b Beschränkt den Match auf den Anfang/das Ende des Wortes +# \1 Referenz auf die erste Gruppe (In dem Fall das erste Vorkommen des Wortes) +# \w+ Matched mindestens ein Wort + +text = re.sub(r'\b(\w+)( \1\b)+', r'\1', text) +print(text) diff --git a/s2_a3.py b/s2_a3.py new file mode 100644 index 0000000..dc1487f --- /dev/null +++ b/s2_a3.py @@ -0,0 +1,53 @@ +import re +import json + +index = 0 + +def parse_zeile(zeile): + global index + index = index + 1 + + elemente = zeile.split(", ") + name = elemente[0] + geburtsdatum = elemente[1] + adresse = elemente[2] + rufnummer = elemente[3] + print(name, adresse, geburtsdatum, rufnummer) + + person = {} + + person['Index'] = index + + name_matches = re.search(r'([^ ]+) ([^ ]+) ([^ ]+)(?: ([^ ]+))?', name) + person['Titel'] = [name_matches.group(1)] + person['Vorname'] = [name_matches.group(2)] + if name_matches.group(4) is None: + person['Zweitname'] = [None] + person['Nachname'] = [name_matches.group(3)] + else: + person['Zweitname'] = [name_matches.group(3)] + person['Nachname'] = [name_matches.group(4)] + + adress_matches = re.search(r'([^0-9]+) ([0-9]+) ([0-9]{5,10}) (.+)', adresse) + person['Straße'] = [adress_matches.group(1)] + person['Hausnummer'] = [adress_matches.group(2)] + person['PLZ'] = [adress_matches.group(3)] + person['Wohnort'] = [adress_matches.group(4)] + + birth_matches = re.search(r'([0-9]{2,4})[ .-]([0-9]{2})[ .-]([0-9]{2,4})', geburtsdatum) + if len(birth_matches.group(1)) == 4: + person['Geburtsdatum'] = [birth_matches.group(3) + "." + birth_matches.group(2) + "." + birth_matches.group(1)] + else: + person['Geburtsdatum'] = [birth_matches.group(1) + "." + birth_matches.group(2) + "." + birth_matches.group(3)] + + return person + + +personen = [] + +with open("Personen.txt", 'r') as file: + for zeile in file: + personen.append(parse_zeile(zeile)) + +with open("PersonenNeu.json", 'w') as file: + json.dump(personen, file) diff --git a/s2_a4_a.py b/s2_a4_a.py new file mode 100644 index 0000000..e0ba688 --- /dev/null +++ b/s2_a4_a.py @@ -0,0 +1,21 @@ +import re + +def parse_number(number): + pattern = r'^(?:\+?1[- ])?(\(\d{3}\)|\d{3})[-. ](\d{3})[-. ](\d{4})$' + + if not re.fullmatch(pattern, number): + raise ValueError("Ungültige Telefonnummer") + + segments = re.search(pattern, number) + + ortsvorwahl = segments.group(1).replace("(", "").replace(")", "") + amtskennziffer = segments.group(2) + vorwahl = segments.group(3) + + if ortsvorwahl[0] <= '1' and ortsvorwahl[0] >= '0': + raise ValueError("Ungültige Telefonnummer") + + if (vorwahl[0] <= '1' and vorwahl[0] >= '0') or vorwahl[1] == '9': + raise ValueError("Ungültige Telefonnummer") + + return "1-" + ortsvorwahl + "-" + amtskennziffer + "-" + vorwahl diff --git a/s2_a4_b.py b/s2_a4_b.py new file mode 100644 index 0000000..7104338 --- /dev/null +++ b/s2_a4_b.py @@ -0,0 +1,33 @@ +import unittest +import s2_a4_a + +test_numbers = [ + ("+1 223-456-7890", "1-223-456-7890"), + ("1-223-456-7890", "1-223-456-7890"), + ("+1 223 456-7890", "1-223-456-7890"), + ("(223) 456-7890", "1-223-456-7890"), + ("1 223 456 7890", "1-223-456-7890"), + ("223.456.7890", "1-223-456-7890"), + ("1-989-111-2222", "1-989-111-2222"), +] + +test_numbers_invalid = [ + "", + "+49 012 345 6821", + "+49 6821", + "1-182-324-4324", + "1-082-324-4324", + "1-682-624-1324", + "1-682-624-0324", + "1-682-624-5924", +] + + +class NumbersTests(unittest.TestCase): + def test_numbers(self): + for number, expected in test_numbers: + self.assertEqual(expected, s2_a4_a.parse_number(number)) + + def test_numbers_fails(self): + for number in test_numbers_invalid: + self.assertRaises(ValueError, lambda: s2_a4_a.parse_number(number))