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
6 changes: 6 additions & 0 deletions src/demo_toolkit/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 5 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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