Skip to content

Commit

Permalink
add lab3 to informatics
Browse files Browse the repository at this point in the history
  • Loading branch information
NF-coder committed Nov 27, 2024
1 parent eefb6bc commit 45e6e51
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 0 deletions.
File renamed without changes.
Binary file not shown.
80 changes: 80 additions & 0 deletions informatics/sem1/lab3/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from lib import Test, Dataset

class DB:
Task1 = Dataset(
Test(
";<) \n ;<) \=;<)",
3
),
Test(
";<);<);<);<);<);<)",
6
),
Test(
";<)\;<)\l;<)",
3
),
Test(
";<)fghjk;;;;;:;;<))))) m:<(",
2
),
Test(
";<)fcclkkyuaew8;<)",
2
),
name = "Tsk1"
)
Task2 = Dataset(
Test(
"Довольно распространённая ошибка ошибка – это лишний повтор повтор слова слова. Смешно, не не правда ли? Не нужно портить хор хоровод.",
"Довольно распространённая ошибка – это лишний повтор слова. Смешно, не правда ли? Не нужно портить хор хоровод."
),
Test(
"W W W W W",
"W"
),
Test(
"W W W W W",
"W"
),
Test(
"a a",
"a"
),
Test(
"a а",
"a а",
flags=["SPACES_MODE"]
),
name = "Tsk2"
)
Task3 = Dataset(
Test(
"Классное слово – обороноспособность, которое должно идти после слов: трава и молоко.",
"""и
идти
слов
слово
трава
должно
молоко
обороноспособность"""
),
Test(
"Е а",
\nЕ"
),
Test(
"АА а ААА",
\nАА\nААА"
),
Test(
"СССР КПСС",
""
),
Test(
"мне лень придумывать тест",
"мне\nлень\nтест"
),
name = "Tsk3"
)
6 changes: 6 additions & 0 deletions informatics/sem1/lab3/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .regex.main import Main as regex

from .test.main import Main as test

from .test.dataset.test_obj import Test
from .test.dataset.dataset import Dataset
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 14 additions & 0 deletions informatics/sem1/lab3/lib/regex/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import re

class Main():
def __init__(self, smile: str = r"\;\<\)") -> None:
self.regex = re.compile(smile)

def count_matches(self, inp: str) -> int:
return len(re.findall(self.regex, inp))

def findall(self, inp: str) -> list:
return re.findall(self.regex, inp)

def sub(self, inp: str) -> str:
return re.sub(self.regex, "", inp)
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions informatics/sem1/lab3/lib/test/dataset/dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .test_obj import Test

class Dataset():
def __init__(self, *args, name: str = "Test") -> None:
self.tests_array = args
self.name = name

def __len__(self):
return len(self.tests_array)

def __getitem__(self, idx):
_ = self.tests_array[idx]
return (idx+1, _.test, _.result, _.flags)
10 changes: 10 additions & 0 deletions informatics/sem1/lab3/lib/test/dataset/test_obj.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from dataclasses import dataclass
from typing import List, Union, Optional

@dataclass
class Test:
test: str
result: str | int

# Possible flags - IGNORE_ERROR, SPACES_MODE
flags: Optional[Union[List[str]]] = None
38 changes: 38 additions & 0 deletions informatics/sem1/lab3/lib/test/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Main():
def __init__(self, tests: object, test_name: str = None) -> None:
self.tests = tests
self.test_name = test_name if test_name is not None else tests.name

def run(self, func: object) -> None:

def inner() -> int:
print(
f"\n{func.__name__} тестируется на наборе тестов {self.test_name}"
)
for idx, test, result, flags in self.tests:
if func(test) == result: print(f"\t✅ Тест {idx} пройден успешно")
else:
print(f"\t❌ Тест {idx} провален")

if flags is not None and "SPACES_MODE" in flags:
R, L = "<", ">"
else: R, L = "", ""

print(
"\t\t" + f"Ввод: {L}{test}{R}",
f"Ожидаемый результат: {L}{result}{R}",
f"Полученный результат: {L}{func(test)}{R}",
sep = "\n\t\t"
)


if flags is not None and "IGNORE_ERROR" in flags:
print("\t\tIGNORE_ERROR flag activated!")
continue

break
else: return -1
return idx

_ = inner()
print("✅ Ошибок не обнаружено!" if _==-1 else f"❌ Ошибка на тесте {_}!")
59 changes: 59 additions & 0 deletions informatics/sem1/lab3/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from lib import regex, test
from db import DB

def main() -> None:
# Task 1
Regex1 = regex(r"\;\<\)")
@test(DB.Task1).run
def count_smiles(inp):
return Regex1.count_matches(inp)

def count_smiles1(inp):
return Regex1.count_matches(inp)

# Task 2
Regex2 = regex(r"\b(\w+)\s+(?=\1(\b))")
@test(DB.Task2).run
def del_repeats(inp):
return Regex2.sub(inp)

def del_repeats1(inp):
return Regex2.sub(inp)

# Task 3 - только русский!
Regex3 = regex(r"(?i)\b([^оуэыияеёю ]+|[^ауэыияеёю ]+|[^аоэыияеёю ]+|[^аоуыияеёю ]+|[^аоуэияеёю ]+|[^аоуэыяеёю ]+|[^аоуэыиеёю ]+|[^аоуэыияёю ]+|[^аоуэыияею ]+|[^аоуэыияеё ]+)\b")
Regex4 = regex(r"(?i)\b[^оуэыияеёюа ]+\b")
@test(DB.Task3).run
def vowel_find(inp):
words = list(set(Regex3.findall(inp)) - set(Regex4.findall(inp)))
return "\n".join(
sorted(
words,
key=lambda x: (len(x), x.lower())
)
)

def vowel_find1(inp):
words = list(set(Regex3.findall(inp)) - set(Regex4.findall(inp)))
return "\n".join(
sorted(
words,
key=lambda x: (len(x), x.lower())
)
)

print()

print("MANUAL TEST MODE")
regex_array = [count_smiles1, del_repeats1, vowel_find1]
while True:
_ = input("Введите номер регулярки: ")
inp = input("Введите текст: ")

if _ == "1": print(regex_array[int(_)-1](inp))
elif _ == "2": print(regex_array[int(_)-1](inp))
elif _ == "3": print(regex_array[int(_)-1](inp))
else: print("Нет такой регулярки")

if __name__ == "__main__":
main()
Binary file added informatics/sem1/lab4/4.odt
Binary file not shown.
File renamed without changes.

0 comments on commit 45e6e51

Please sign in to comment.