jfz-2023-s473616/TaskA04/run.py

51 lines
1.6 KiB
Python

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