Compare commits
5 Commits
a80acb36c3
...
df8d292a6e
| Author | SHA1 | Date | |
|---|---|---|---|
| df8d292a6e | |||
| 7e76456599 | |||
| 4940dfeffd | |||
| c5999afcad | |||
| 3ad3dc517f |
@ -25,11 +25,21 @@ def clamp_to_byte(value: int) -> int:
|
|||||||
return max(0, min(255, value))
|
return max(0, min(255, value))
|
||||||
|
|
||||||
|
|
||||||
def variance(values: Sequence[float]) -> float:
|
def average(values: list[int | float]) -> float:
|
||||||
if not values:
|
if not values:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
mean = sum(values) / len(values)
|
return sum(values) / len(values)
|
||||||
return sum((x - mean) ** 2 for x in values) / len(values)
|
|
||||||
|
|
||||||
|
def median(values: list[float]) -> float:
|
||||||
|
if not values:
|
||||||
|
raise ValueError
|
||||||
|
sorted_values = sorted(values)
|
||||||
|
n = len(sorted_values)
|
||||||
|
mid = n // 2
|
||||||
|
if n % 2 == 1:
|
||||||
|
return sorted_values[mid]
|
||||||
|
return (sorted_values[mid - 1] + sorted_values[mid]) / 2.0
|
||||||
|
|
||||||
|
|
||||||
def percentage(value: Union[int, float], total: Union[int, float]) -> float:
|
def percentage(value: Union[int, float], total: Union[int, float]) -> float:
|
||||||
@ -38,4 +48,9 @@ def percentage(value: Union[int, float], total: Union[int, float]) -> float:
|
|||||||
return (value / total) * 100
|
return (value / total) * 100
|
||||||
|
|
||||||
|
|
||||||
|
def variance(values: Sequence[float]) -> float:
|
||||||
|
if not values:
|
||||||
|
raise ValueError
|
||||||
|
mean = sum(values) / len(values)
|
||||||
|
return sum((x - mean) ** 2 for x in values) / len(values)
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# retoor <retoor@molodetz.nl>
|
# retoor <retoor@molodetz.nl>
|
||||||
|
|
||||||
from typosaurus_sandbox.domain.calculator.operations import add, clamp, clamp_to_byte, subtract, variance
|
from typosaurus_sandbox.domain.calculator.operations import add, average, clamp, clamp_to_byte, median, percentage, subtract, variance
|
||||||
|
|
||||||
__all__ = ["add", "subtract", "clamp", "clamp_to_byte", "variance"]
|
__all__ = ["add", "average", "clamp", "clamp_to_byte", "median", "percentage", "subtract", "variance"]
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# retoor <retoor@molodetz.nl>
|
# retoor <retoor@molodetz.nl>
|
||||||
|
|
||||||
from typing import Sequence
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
|
||||||
def add(left: int, right: int) -> int:
|
def add(left: int, right: int) -> int:
|
||||||
@ -25,9 +25,32 @@ def clamp_to_byte(value: int) -> int:
|
|||||||
return max(0, min(255, value))
|
return max(0, min(255, value))
|
||||||
|
|
||||||
|
|
||||||
|
def average(values: list[int | float]) -> float:
|
||||||
|
if not values:
|
||||||
|
raise ValueError
|
||||||
|
return sum(values) / len(values)
|
||||||
|
|
||||||
|
|
||||||
|
def median(values: list[float]) -> float:
|
||||||
|
if not values:
|
||||||
|
raise ValueError
|
||||||
|
sorted_values = sorted(values)
|
||||||
|
n = len(sorted_values)
|
||||||
|
mid = n // 2
|
||||||
|
if n % 2 == 1:
|
||||||
|
return sorted_values[mid]
|
||||||
|
return (sorted_values[mid - 1] + sorted_values[mid]) / 2.0
|
||||||
|
|
||||||
|
|
||||||
def variance(values: Sequence[float]) -> float:
|
def variance(values: Sequence[float]) -> float:
|
||||||
if not values:
|
if not values:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
mean = sum(values) / len(values)
|
mean = sum(values) / len(values)
|
||||||
return sum((x - mean) ** 2 for x in values) / len(values)
|
return sum((x - mean) ** 2 for x in values) / len(values)
|
||||||
|
|
||||||
|
|
||||||
|
def percentage(value: Union[int, float], total: Union[int, float]) -> float:
|
||||||
|
if total == 0:
|
||||||
|
raise ValueError
|
||||||
|
return (value / total) * 100
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
import math
|
import math
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from typosaurus_sandbox.domain.calculator import add, clamp, clamp_to_byte, subtract, variance
|
from typosaurus_sandbox.domain.calculator import add, average, clamp, clamp_to_byte, median, subtract, variance
|
||||||
|
|
||||||
|
|
||||||
class TestAddFunction(unittest.TestCase):
|
class TestAddFunction(unittest.TestCase):
|
||||||
@ -18,6 +18,28 @@ class TestAddFunction(unittest.TestCase):
|
|||||||
self.assertEqual(add(-3, 5), 2)
|
self.assertEqual(add(-3, 5), 2)
|
||||||
|
|
||||||
|
|
||||||
|
class TestAverageFunction(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_empty_sequence_raises_value_error(self) -> None:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
average([])
|
||||||
|
|
||||||
|
def test_single_element(self) -> None:
|
||||||
|
self.assertEqual(average([5]), 5.0)
|
||||||
|
|
||||||
|
def test_positive_values(self) -> None:
|
||||||
|
self.assertEqual(average([1, 2, 3, 4, 5]), 3.0)
|
||||||
|
|
||||||
|
def test_negative_values(self) -> None:
|
||||||
|
self.assertEqual(average([-10, -20, -30]), -20.0)
|
||||||
|
|
||||||
|
def test_mixed_positive_and_negative(self) -> None:
|
||||||
|
self.assertEqual(average([-5, 0, 5]), 0.0)
|
||||||
|
|
||||||
|
def test_float_values(self) -> None:
|
||||||
|
self.assertEqual(average([1.5, 2.5, 3.0]), 7.0 / 3.0)
|
||||||
|
|
||||||
|
|
||||||
class TestSubtractFunction(unittest.TestCase):
|
class TestSubtractFunction(unittest.TestCase):
|
||||||
|
|
||||||
def test_subtract_positive(self) -> None:
|
def test_subtract_positive(self) -> None:
|
||||||
@ -103,6 +125,32 @@ class TestClampFunction(unittest.TestCase):
|
|||||||
self.assertTrue(math.isnan(result))
|
self.assertTrue(math.isnan(result))
|
||||||
|
|
||||||
|
|
||||||
|
class TestMedianFunction(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_odd_length_returns_middle_element(self) -> None:
|
||||||
|
self.assertEqual(median([1, 3, 5]), 3)
|
||||||
|
|
||||||
|
def test_even_length_returns_float_average_of_two_middle_values(self) -> None:
|
||||||
|
result = median([1, 2, 3, 4])
|
||||||
|
self.assertIsInstance(result, float)
|
||||||
|
self.assertEqual(result, 2.5)
|
||||||
|
|
||||||
|
def test_single_element_returns_that_element(self) -> None:
|
||||||
|
self.assertEqual(median([7]), 7)
|
||||||
|
|
||||||
|
def test_empty_list_raises_value_error(self) -> None:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
median([])
|
||||||
|
|
||||||
|
def test_unsorted_input_sorts_correctly(self) -> None:
|
||||||
|
self.assertEqual(median([3, 1, 2]), 2)
|
||||||
|
|
||||||
|
def test_unsorted_even_length_returns_float_average(self) -> None:
|
||||||
|
result = median([10, 30, 20, 40])
|
||||||
|
self.assertIsInstance(result, float)
|
||||||
|
self.assertEqual(result, 25.0)
|
||||||
|
|
||||||
|
|
||||||
class TestVarianceFunction(unittest.TestCase):
|
class TestVarianceFunction(unittest.TestCase):
|
||||||
|
|
||||||
def test_empty_list_raises_value_error(self) -> None:
|
def test_empty_list_raises_value_error(self) -> None:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user