This commit is contained in:
Pawel Felcyn 2023-10-28 18:05:08 +02:00
parent 0c23da0e15
commit ee561d0f06
4 changed files with 203 additions and 0 deletions

20
TaskA01/run.py Normal file
View File

@ -0,0 +1,20 @@
def contains_word(to_be_checked: str, word: str) -> bool:
word_len: int = len(word)
word_character_number: int = 0
for c in to_be_checked:
if c != word[word_character_number]:
word_character_number = 0
continue
if word_character_number == word_len - 1:
return True
word_character_number += 1
return False
file = open('shakespeare.exp', 'r', encoding='utf8')
result = []
line_number = 0
for line in file:
if contains_word(line, 'Hamlet'):
result.append(line_number)
line_number += 1
print(result)

62
TaskA02/run.py Normal file
View File

@ -0,0 +1,62 @@
class Machine:
_state: int = 0
def consume_character(self, char: str) -> None:
if self._state == 0:
if char == 'P' or char == 'p':
self._state = 1
else:
self._state = 6
elif self._state == 1:
if char == 'I' or char == 'i':
self._state = 2
else:
self._state = 6
elif self._state == 2:
if char == 'E' or char == 'e':
self._state = 3
else:
self._state = 6
elif self._state == 3:
if char == 'S' or char == 's':
self._state = 4
else:
self._state = 6
elif self._state == 4:
if char == '\n' or char == ' ' or char == '\t' or char == '\r':
self._state = 5
else:
self._state = 6
elif self._state == 6:
if char == ' ' or char == '\t':
self._state = 0
def is_success_state(self) -> bool:
return self._state == 5
def restart(self) -> None:
self._state = 0
def find_lines_with_pies(text: str) -> list[int]:
output: list[int] = []
line_number = 0
machine = Machine()
for char in text:
machine.consume_character(char)
if char == '\n':
if machine.is_success_state():
output.append(line_number)
machine.restart()
line_number += 1
if machine.is_success_state():
output.append(line_number)
line_number += 1
return output
text: str = None
with open('polish_wiki_excerpt.exp', 'r', encoding='utf8') as file:
text = file.read()
result = find_lines_with_pies(text)
print(result)

70
TaskA03/run.py Normal file
View File

@ -0,0 +1,70 @@
class Machine:
_state: int = 0
def consume_character(self, char: str) -> None:
if self._state == 0:
if char == '1':
self._state = 1
else:
self._state = 0
elif self._state == 1:
if char == '9':
self._state = 2
else:
self._state = 0
elif self._state == 2:
if char == '1' or char == '2' or char == '3' or char == '4' or char == '5' or char == '6' or char == '7' or char == '8' or char == '9':
self._state = 3
else:
self._state = 0
elif self._state == 3:
if char == '1' or char == '2' or char == '3' or char == '4' or char == '5' or char == '6' or char == '7' or char == '8' or char == '9':
self._state = 4
else:
self._state = 0
elif self._state == 4:
if char == ' ':
self._state = 5
else:
self._state = 0
elif self._state == 5:
if char == 'r':
self._state = 6
else:
self._state = 0
elif self._state == 6:
if char == '.':
self._state = 7
else:
self._state = 0
def is_success_state(self) -> bool:
return self._state == 7
def restart(self) -> None:
self._state = 0
def find_lines_with_date(text: str) -> list[int]:
output: list[int] = []
line_number = 0
machine = Machine()
for char in text:
machine.consume_character(char)
if char == '\n':
if machine.is_success_state():
output.append(line_number)
machine.restart()
line_number += 1
if machine.is_success_state():
output.append(line_number)
line_number += 1
return output
text: str = None
with open('polish_wiki_excerpt.exp', 'r', encoding='utf8') as file:
text = file.read()
result = find_lines_with_date(text)
print(result)

51
TaskA04/run.py Normal file
View File

@ -0,0 +1,51 @@
class Machine:
_state: int = 0
_current_digital_substring: str = ''
_results: list[str] = []
def consume_character(self, char: str) -> None:
if self._state == 0:
if self._is_digit(char):
self._state = 1
self._current_digital_substring += char
elif not self._is_separator(char):
self.state = 2
elif self._state == 1:
if self._is_digit(char):
self._current_digital_substring += char
elif not self._is_separator(char):
self.state = 2
self._current_digital_substring = ''
else:
self.finish()
elif self._state == 2:
if self._is_separator(char):
self.state = 0
def _is_separator(self, char) -> bool:
return char == '\n' or char == ' ' or char == '\t'
def _is_digit(self, char: str) -> bool:
return char == '1' or char == '2' or char == '3' or char == '4' or char == '5' or char == '6' or char == '7' or char == '8' or char == '9'
def finish(self) -> None:
if self._current_digital_substring != '':
self._results.append(self._current_digital_substring)
self._current_digital_substring = ''
self._state = 0
def get_results(self) -> list[int]:
return self._results
text: str = None
with open('simple.exp', 'r', encoding='utf8') as file:
text = file.read()
machine = Machine()
for c in text:
machine.consume_character(c)
machine.finish()
print(machine.get_results())