diff --git a/src/demo_toolkit/numbers.py b/src/demo_toolkit/numbers.py index 2f35406..107b048 100644 --- a/src/demo_toolkit/numbers.py +++ b/src/demo_toolkit/numbers.py @@ -7,3 +7,9 @@ def clamp(value: float, minimum: float, maximum: float) -> float: if minimum > maximum: raise ValueError("minimum cannot be greater than maximum") return min(maximum, max(minimum, value)) + +def calculate_discount(price: float, discount_percent: float) -> float: + """Calculate the final price after applying a percentage discount.""" + if discount_percent < 0 or discount_percent > 100: + raise ValueError("Discount percent must be between 0 and 100.") + return price * (1 - discount_percent / 100.0) \ No newline at end of file diff --git a/tests/test_numbers.py b/tests/test_numbers.py index df59cbd..73d20c5 100644 --- a/tests/test_numbers.py +++ b/tests/test_numbers.py @@ -1,6 +1,7 @@ import pytest from demo_toolkit.numbers import clamp +from demo_toolkit.numbers import calculate_discount def test_clamp_limits_both_sides(): @@ -11,3 +12,7 @@ def test_clamp_limits_both_sides(): def test_clamp_rejects_reversed_range(): with pytest.raises(ValueError, match="minimum"): clamp(1, 10, 0) + +def test_calculate_discount_applies_percentage(): + assert calculate_discount(100.0, 20.0) == 80.0 + assert calculate_discount(50.0, 10.0) == 45.0