From 03b60ae694140f0a0e7b6c7eb8a4956420bb57d3 Mon Sep 17 00:00:00 2001 From: jwieczor Date: Sun, 17 Nov 2024 23:04:47 +0100 Subject: [PATCH] 3th task --- TaskA03/task03.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 TaskA03/task03.py diff --git a/TaskA03/task03.py b/TaskA03/task03.py new file mode 100644 index 0000000..2f8c618 --- /dev/null +++ b/TaskA03/task03.py @@ -0,0 +1,32 @@ +def is_numeric(s): + for char in s: + if char < '0' or char > '9': + return False + return True + +def find_lines_with_date_from_file(filename): + result = [] + + with open(filename, 'r', encoding='utf-8') as file: + lines = file.readlines() + + for line in lines: + i = 0 + while i < len(line): + if i + 3 < len(line): + year_candidate = line[i:i+4] + if is_numeric(year_candidate): + year = int(year_candidate) + if 1900 <= year <= 1999: + if i + 6 < len(line) and line[i+4] == ' ' and line[i+5:i+7] == 'r.': + result.append(line.strip()) + break + i += 1 + + return result + +filename = 'TaskA03/simple.in' +output = find_lines_with_date_from_file(filename) + +for line in output: + print(line)