|
# retoor <retoor@molodetz.nl>
|
|
|
|
from typing import Sequence, Union
|
|
|
|
|
|
def add(left: int, right: int) -> int:
|
|
return left + right
|
|
|
|
|
|
def subtract(left: int, right: int) -> int:
|
|
return left - right
|
|
|
|
|
|
def clamp(value: int, low: int, high: int) -> int:
|
|
if low > high:
|
|
raise ValueError
|
|
if value < low:
|
|
return low
|
|
if value > high:
|
|
return high
|
|
return value
|
|
|
|
|
|
def clamp_to_byte(value: int) -> int:
|
|
return max(0, min(255, value))
|
|
|
|
|
|
def average(values: list[int | float]) -> float:
|
|
if not values:
|
|
raise ValueError
|
|
return sum(values) / len(values)
|
|
|
|
|
|
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)
|
|
|
|
|
|
def percentage(value: Union[int, float], total: Union[int, float]) -> float:
|
|
if total == 0:
|
|
raise ValueError
|
|
return (value / total) * 100
|
|
|