diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c35007a --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,25 @@ +name: CI + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run tests + run: | + PYTHONPATH=. pytest diff --git a/README.md b/README.md new file mode 100644 index 0000000..fe4760c --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# OtusArchitecture +ДЗ по архитектурным шаблонам diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..55b033e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest \ No newline at end of file diff --git a/solver.py b/solver.py new file mode 100644 index 0000000..acc2f4b --- /dev/null +++ b/solver.py @@ -0,0 +1,28 @@ +import math + +def solve(a, b, c): + # Проверяем, что все коэффициенты являются конечными числами + if not all(map(math.isfinite, [a, b, c])): + raise ValueError("Коэффициенты должны быть конечными.") + + # Проверка на то, что a не равно 0 + if abs(a) < 1e-9: + raise ValueError("Коэффициент а не может быть нулём.") + + # Вычисляем дискриминант + discriminant = b ** 2 - 4 * a * c + + # Сравниваем дискриминант с нулем с учетом эпсилон + epsilon = 1e-9 + if discriminant > epsilon: + # Два различных корня + root1 = (-b + math.sqrt(discriminant)) / (2 * a) + root2 = (-b - math.sqrt(discriminant)) / (2 * a) + return [root1, root2] + elif discriminant < -epsilon: + # Нет корней + return [] + else: + # Один корень кратности два (дискриминант близок к нулю) + root = -b / (2 * a) + return [root] diff --git a/unit tests/test_solver.py b/unit tests/test_solver.py new file mode 100644 index 0000000..a70deb8 --- /dev/null +++ b/unit tests/test_solver.py @@ -0,0 +1,26 @@ +import pytest +from solver import solve + +def test_no_roots(): + assert solve(1, 0, 1) == [] + +def test_two_roots(): + roots = solve(1, 0, -1) + assert len(roots) == 2 + assert 1 in roots and -1 in roots + +def test_one_root(): + assert solve(1, 2, 1) == [-1] + +def test_a_is_zero(): + with pytest.raises(ValueError): + solve(0, 1, 1) + +def test_non_numeric_coefficients(): + with pytest.raises(TypeError): + solve("a", 1, 1) + +def test_discriminant_near_zero(): + # Коэффициенты подобраны так, чтобы дискриминант был меньше 1e-9 + assert solve(1, -2, 1 + 1e-10) == [1] +