diff --git a/TaskC37/description.txt b/TaskC37/description.txt index d6d9c83..fa801d5 100644 --- a/TaskC37/description.txt +++ b/TaskC37/description.txt @@ -22,7 +22,7 @@ bądź NNN-NN-NN. Jeśli napis nie spełnia podanych warunków, należy wypisać "". For each string, extract an area code from the phone number. -We assume, that the phone number is two digits, is preceded by zero or plus. +We assume, that the area code(?) is two digits, is preceded by zero or plus. The rest of the phone number is 7 digits written in N-NNN-NNN or NNN-NN-NN form. If the string does not fulfill the condition, you should print "". diff --git a/TaskC37/solution.py b/TaskC37/solution.py new file mode 100644 index 0000000..87b2a20 --- /dev/null +++ b/TaskC37/solution.py @@ -0,0 +1,14 @@ +import re +import sys + +def extract_area_code(line: str) -> str: + # While this regex seems complicated, it essentially enforces either of two possible formats + # Returning the area code captured in group(1) + p = re.compile(r'^[+0](\d{2})\s(?:\d-\d{3}-\d{3}|\d{3}-\d{2}-\d{2})$') + m = p.match(line) + return m.group(1) if m else "" + +if __name__ == "__main__": + for line in sys.stdin: + stripped_line = line.strip() + print(extract_area_code(stripped_line)) diff --git a/TaskC37/test.out b/TaskC37/test.out new file mode 100644 index 0000000..483e86e --- /dev/null +++ b/TaskC37/test.out @@ -0,0 +1,9 @@ + +61 +61 +61 +61 +82 + + +