feat: MVP проекта закончено

This commit is contained in:
ElectronixTM
2025-03-16 01:41:59 +03:00
parent b044e908bb
commit 90f7569d7a
2 changed files with 82 additions and 9 deletions

View File

@ -4,13 +4,68 @@ this class applies markov algorightm to given string
"""
from dataclasses import dataclass
from collections import OrderedDict
from typing import ClassVar
from rule import Rule, EMPTY_SYMBOL
@dataclass
class Rulebook:
rules: OrderedDict[str, str]
MAX_DEPTH: ClassVar[int] = 100
rules: list[Rule]
def transform(self, string: str) -> str:
"""
Applies the next appropriate rule to given string. The
priority of rules is determined by their indexes in list
of rules. Lesser the index - higher the priority.
Return transformed string
"""
for _ in range(self.MAX_DEPTH):
rule = self.find_appropriate_rule(string)
if rule is None:
return string
string = self._apply_rule(rule, string)
if rule.is_blocking:
return string
raise ValueError("The amount of transformations exceeded "
f"{self.MAX_DEPTH}. You can change maximum "
"amount by setting class variable MAX_DEPTH,"
" but may be something wrong with your input")
def find_appropriate_rule(self, string) -> Rule | None:
"""
Searches for appropriate rule in rules list and if
found one returns it, otherwise returns None
"""
for rule in self.rules:
if self._is_rule_appropriate(rule, string):
return rule
def _apply_rule(self, rule: Rule, string: str) -> str:
"""
Tries to apply given rule to string. Doesn't check
wether the rule is appropriate or not. Inapplicability
leads to undefined behaviour
"""
if rule.operand == EMPTY_SYMBOL:
string = EMPTY_SYMBOL + string
string = (
string
.replace(rule.operand, rule.target, 1)
.replace(EMPTY_SYMBOL, '')
)
return string
def _is_rule_appropriate(self, rule: Rule, string: str) -> bool:
"""
Concrete realization of rule applicability check. Moved to separate
function mostly for extensibility purposes
"""
# Hardest part is always about empty set
if rule.operand == EMPTY_SYMBOL:
return True
return rule.operand in string
def __call__(self, string: str):
"""aplies rule to the given string"""
raise NotImplementedError("Sorry, we still don't know how to apply"
"algorithm to your string")