From 27bcde8f093d1e13c0f80b0a654bbe95884be61e Mon Sep 17 00:00:00 2001 From: mxsgd Date: Sun, 7 Jan 2024 21:21:03 +0100 Subject: [PATCH] F --- TaskC00/run.py | 33 ++++++++++++++-------------- TaskF00/run.py | 18 ++++++++++++++++ TaskF01/run.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ TaskF02/run.py | 40 ++++++++++++++++++++++++++++++++++ TaskF03/run.py | 13 +++++++++++ TaskF04/run.py | 11 ++++++++++ TaskF05/run.py | 26 ++++++++++++++++++++++ run_report.py | 4 ++-- 8 files changed, 184 insertions(+), 19 deletions(-) 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/TaskC00/run.py b/TaskC00/run.py index 64ecf48..c77ea28 100644 --- a/TaskC00/run.py +++ b/TaskC00/run.py @@ -1,5 +1,5 @@ import sys - +import copy class FSA: @@ -20,28 +20,27 @@ class FSA: else: self.transitions[state_from] = dict() self.transitions[state_from][symbol] = {state_to} - def get_final_state(self, string, start_state): + def get_final_state(self, string): - current_state = start_state + current_states = {self.initial_state} for symbol in string: - try: - for state in self.transitions[current_state][symbol]: - current_state = self.get_final_states(string[1:], state) - if current_state in self.final_states: - return current_state - except KeyError: - return -1 - return current_state + new_current_states = set() + for current_state in current_states: + try: + new_current_states |= self.transitions[current_state][symbol] + except: + pass + current_states = copy.deepcopy(new_current_states) + return current_states def add_final_state(self, state): self.final_states.add(state) def accepts(self, string): - - if self.get_final_state(string, self.initial_state) in self.final_states: - return True - else: - return False - + final_states = self.get_final_states(string) + for final_state in final_states: + if final_state in self.final_states: + return True + return False fsa = FSA() table = open(sys.argv[1]) diff --git a/TaskF00/run.py b/TaskF00/run.py new file mode 100644 index 0000000..e14d9d6 --- /dev/null +++ b/TaskF00/run.py @@ -0,0 +1,18 @@ +import sys, re + +for line in sys.stdin: + line = line.strip() + list_words = re.findall(r'[0-9]{4}', line) + for word in list_words: + modified_word = re.sub('0', 'a', word) + modified_word = re.sub('1', 'b', modified_word) + modified_word = re.sub('2', 'c', modified_word) + modified_word = re.sub('3', 'd', modified_word) + modified_word = re.sub('4', 'e', modified_word) + modified_word = re.sub('5', 'f', modified_word) + modified_word = re.sub('6', 'g', modified_word) + modified_word = re.sub('7', 'h', modified_word) + modified_word = re.sub('8', 'i', modified_word) + modified_word = re.sub('9', 'j', modified_word) + line = re.sub(word, modified_word, line) + print(line) \ No newline at end of file diff --git a/TaskF01/run.py b/TaskF01/run.py new file mode 100644 index 0000000..e5531c2 --- /dev/null +++ b/TaskF01/run.py @@ -0,0 +1,58 @@ +import re +import sys + +delimiters = ["'", '"', '’', '-', ':', "|", ".", ",", " "] +def split_with_multiple_delimiters(text): + list = [] + previousI = 0 + for i, char in enumerate(text): + if char in delimiters: + list.append(text[previousI:i]) + previousI = i + if i == len(text)-1: + list.append(text[previousI:i+1]) + return list + + +def convert_case(original_line): + words = original_line.split() + changed_line = r'' + for word in words: + word = word.strip() + split_word = split_with_multiple_delimiters(word) + for s_word in split_word: + converted_word = r'' + lower_set = False + upper_set = False + for char in s_word: + if re.match(r'[a-ząćęłńóśźż]', char): + lower_set = True + elif re.match(r'[A-ZĄĆĘŁŃÓŚŹŻ]', char): + upper_set = True + else: + continue + if upper_set and lower_set: + for char in s_word: + if char.islower(): + if re.match(r'[a-ząćęłńóśźż]', char): + converted_word += char.upper() + else: + converted_word += char + elif char.isupper(): + if re.match(r'[A-ZĄĆĘŁŃÓŚŹŻ]', char): + converted_word += char.lower() + else: + converted_word += char + else: + converted_word += char + changed_line += converted_word + else: + changed_line += s_word + changed_line += ' ' + return changed_line + + +for line in sys.stdin: + line = line.strip('\n') + converted_line = convert_case(line).strip() + print(converted_line) \ No newline at end of file diff --git a/TaskF02/run.py b/TaskF02/run.py new file mode 100644 index 0000000..50cc3dc --- /dev/null +++ b/TaskF02/run.py @@ -0,0 +1,40 @@ +import re +import sys + +lowercasePattern = "[a-ząćęłńóśźż]" +uppercasePattern = "[A-ZĄĆĘŁŃÓŚŹŻ]" +digitPattern = "[0-9]" + + +def isUpperCase(inp: str): + return re.match(uppercasePattern, inp) + + +def isLowerCase(inp: str): + return re.match(lowercasePattern, inp) + + +def isDigit(inp: str): + return re.match(digitPattern, inp) + + +for line in sys.stdin: + line = line.strip('\n') + lower = 0 + upper = 0 + digits = 0 + other = 0 + + for char in line: + if isLowerCase(char): + lower += 1 + elif isUpperCase(char): + upper += 1 + elif isDigit(char): + digits += 1 + else: + other += 1 + + print(f"{lower} {upper} {digits} {other}") + + diff --git a/TaskF03/run.py b/TaskF03/run.py new file mode 100644 index 0000000..44cde33 --- /dev/null +++ b/TaskF03/run.py @@ -0,0 +1,13 @@ +import re +import sys + +startLowerPattern = r'\b[a-ząćęłńóśźż]\w*\b' +startUpperPattern = r'\b[A-ZĄĆĘŁŃÓŚŹŻ]\w*\b' + +for line in sys.stdin: + line = line.strip() + lower = re.findall(startLowerPattern, line) + upper = re.findall(startUpperPattern, line) + + print(f"{len(lower)} {len(upper)}") + diff --git a/TaskF04/run.py b/TaskF04/run.py new file mode 100644 index 0000000..dff60bf --- /dev/null +++ b/TaskF04/run.py @@ -0,0 +1,11 @@ +import re +import sys + + +def deleteSecondDigitString(line: str): + print(re.sub(r"(\D*)(\d+)(\D+)(\d+)(.*)", r"\g<1>\g<2>\g<3>\g<5>", line)) + + +for line in sys.stdin: + line = line.strip('\n') + deleteSecondDigitString(line) diff --git a/TaskF05/run.py b/TaskF05/run.py new file mode 100644 index 0000000..47c5e17 --- /dev/null +++ b/TaskF05/run.py @@ -0,0 +1,26 @@ +import re +import sys + + +def replaceThirdWordWithX(input): + group1 = input.group(1) + empty1 = input.group(2) + group2 = input.group(3) + empty2 = input.group(4) + group3 = "x" * (len(input.group(5))) + rest = input.group(6) + return f'{group1}{empty1}{group2}{empty2}{group3}{rest}' + + +pattern = r'(\w+)(\W+)(\w+)(\W+)(\w+)(.*)' + +for line in sys.stdin: + line = line.strip('\n') + + match = re.match(pattern, line) + if match: + result = replaceThirdWordWithX(match) + print(re.sub(pattern, result, line)) + else: + print(line) + diff --git a/run_report.py b/run_report.py index 402ce32..e640595 100644 --- a/run_report.py +++ b/run_report.py @@ -58,7 +58,7 @@ def is_task_correct(dir): def does_task_match_index(dir, index): - with open(Path(dir, f'description.txt')) as f_in: + with open(Path(dir, f'description.txt'), encoding='UTF-8') as f_in: lines = f_in.readlines() remainder_lines = [x for x in lines if x.startswith('REMAINDER')] if len(remainder_lines) == 0: @@ -71,7 +71,7 @@ def does_task_match_index(dir, index): def get_tasks(index): - all_tasks = Path('../djfz-2023-s464933').glob('TaskC06') + all_tasks = Path('.').glob('Task*') return [task for task in all_tasks if does_task_match_index(task, index)]