This commit is contained in:
mxsgd 2024-01-07 21:21:03 +01:00
parent 841655da18
commit 27bcde8f09
8 changed files with 184 additions and 19 deletions

View File

@ -1,5 +1,5 @@
import sys import sys
import copy
class FSA: class FSA:
@ -20,28 +20,27 @@ class FSA:
else: else:
self.transitions[state_from] = dict() self.transitions[state_from] = dict()
self.transitions[state_from][symbol] = {state_to} 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: for symbol in string:
new_current_states = set()
for current_state in current_states:
try: try:
for state in self.transitions[current_state][symbol]: new_current_states |= self.transitions[current_state][symbol]
current_state = self.get_final_states(string[1:], state) except:
if current_state in self.final_states: pass
return current_state current_states = copy.deepcopy(new_current_states)
except KeyError: return current_states
return -1
return current_state
def add_final_state(self, state): def add_final_state(self, state):
self.final_states.add(state) self.final_states.add(state)
def accepts(self, string): def accepts(self, string):
final_states = self.get_final_states(string)
if self.get_final_state(string, self.initial_state) in self.final_states: for final_state in final_states:
if final_state in self.final_states:
return True return True
else:
return False return False
fsa = FSA() fsa = FSA()
table = open(sys.argv[1]) table = open(sys.argv[1])

18
TaskF00/run.py Normal file
View File

@ -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)

58
TaskF01/run.py Normal file
View File

@ -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)

40
TaskF02/run.py Normal file
View File

@ -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}")

13
TaskF03/run.py Normal file
View File

@ -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)}")

11
TaskF04/run.py Normal file
View File

@ -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)

26
TaskF05/run.py Normal file
View File

@ -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)

View File

@ -58,7 +58,7 @@ def is_task_correct(dir):
def does_task_match_index(dir, index): 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() lines = f_in.readlines()
remainder_lines = [x for x in lines if x.startswith('REMAINDER')] remainder_lines = [x for x in lines if x.startswith('REMAINDER')]
if len(remainder_lines) == 0: if len(remainder_lines) == 0:
@ -71,7 +71,7 @@ def does_task_match_index(dir, index):
def get_tasks(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)] return [task for task in all_tasks if does_task_match_index(task, index)]