From 36127d0d129970255bc00632666d808dc3afb8d6 Mon Sep 17 00:00:00 2001 From: Wojtek Date: Sat, 20 Jan 2024 16:39:21 +0100 Subject: [PATCH] tasks F --- TaskF00/run.py | 14 ++++++++++++++ TaskF01/run.py | 15 +++++++++++++++ TaskF02/run.py | 17 +++++++++++++++++ TaskF03/run.py | 14 ++++++++++++++ TaskF04/run.py | 17 +++++++++++++++++ TaskF05/run.py | 18 ++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 TaskF00/run.py create mode 100644 TaskF01/run.py create mode 100644 TaskF02/run.py create mode 100644 TaskF03/run.py create mode 100644 TaskF04/run.py create mode 100644 TaskF05/run.py diff --git a/TaskF00/run.py b/TaskF00/run.py new file mode 100644 index 0000000..523072d --- /dev/null +++ b/TaskF00/run.py @@ -0,0 +1,14 @@ +import re + +def substitute(file_path): + def replace(match): + return ''.join(chr(ord('a') + int(digit)) for digit in match.group(0)) + + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + substituted_line = re.sub(r'\b\d{4}\b', replace, line.strip()) + print(substituted_line) + + +file_path = 'TaskF00/simple.in' +substitute(file_path) \ No newline at end of file diff --git a/TaskF01/run.py b/TaskF01/run.py new file mode 100644 index 0000000..c88637a --- /dev/null +++ b/TaskF01/run.py @@ -0,0 +1,15 @@ +import re + +def swap(file_path): + pattern = r'\b(?=\w*[a-ząćęłńóśźż])(?=\w*[A-ZĄĆĘŁŃÓŚŹŻ])\w+\b' + + def swap_case(match): + return match.group(0).swapcase() + + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + swapped_line = re.sub(pattern, swap_case, line) + print(swapped_line, end='') + +file_path = 'TaskF01/simple.in' +swap(file_path) \ No newline at end of file diff --git a/TaskF02/run.py b/TaskF02/run.py new file mode 100644 index 0000000..0301078 --- /dev/null +++ b/TaskF02/run.py @@ -0,0 +1,17 @@ +import re + +def count_types(file_path): + lowercase_pattern = r'[a-ząćęłńóśźż]' + uppercase_pattern = r'[A-ZĄĆĘŁŃÓŚŹŻ]' + digit_pattern = r'\d' + + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + a = len(re.findall(lowercase_pattern, line)) + b = len(re.findall(uppercase_pattern, line)) + c = len(re.findall(digit_pattern, line)) + d = len(line) - a - b - c - line.count('\n') + print(f"{a} {b} {c} {d}") + +file_path = 'TaskF02/simple.in' +count_types(file_path) \ No newline at end of file diff --git a/TaskF03/run.py b/TaskF03/run.py new file mode 100644 index 0000000..c4be44d --- /dev/null +++ b/TaskF03/run.py @@ -0,0 +1,14 @@ +import re + +def count(file_path): + lowercase= r'\b[a-ząćęłńóśźż]\w*' + uppercase = r'\b[A-ZĄĆĘŁŃÓŚŹŻ]\w*' + + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + lowercase_count = len(re.findall(lowercase, line)) + uppercase_count = len(re.findall(uppercase, line)) + print(f"{lowercase_count} {uppercase_count}") + +file_path = 'TaskF03/simple.in' +count(file_path) \ No newline at end of file diff --git a/TaskF04/run.py b/TaskF04/run.py new file mode 100644 index 0000000..bda854e --- /dev/null +++ b/TaskF04/run.py @@ -0,0 +1,17 @@ +import re + +def delete(file_path): + digit_pattern = r'\d+' + + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + matches = list(re.finditer(digit_pattern, line)) + + if len(matches) >= 2: + start, end = matches[1].span() + line = line[:start] + line[end:] + + print(line, end='') + +file_path = 'TaskF04/simple.in' +delete(file_path) \ No newline at end of file diff --git a/TaskF05/run.py b/TaskF05/run.py new file mode 100644 index 0000000..0d9772b --- /dev/null +++ b/TaskF05/run.py @@ -0,0 +1,18 @@ +import re + +def replace(file_path): + word_pattern = r'\b\w+\b' + + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + words = list(re.finditer(word_pattern, line)) + + if len(words) >= 3: + start, end = words[2].span() + word_length = end - start + line = line[:start] + 'x' * word_length + line[end:] + + print(line, end='') + +file_path = 'TaskF05/simple.in' +replace(file_path) \ No newline at end of file