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)