Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# OtusArchitecture
ДЗ по архитектурным шаблонам
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
28 changes: 28 additions & 0 deletions solver.py
Original file line number Diff line number Diff line change
@@ -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]
26 changes: 26 additions & 0 deletions unit tests/test_solver.py
Original file line number Diff line number Diff line change
@@ -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]