Spaces:
Runtime error
Runtime error
Download src/utils/math.py from pnstack/template-python: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/pnstack/template-python/resolve/92ff6e639b4691e032a94120f579f17520ba1414/src/utils/math.py
- Command line
-
hf download hf://spaces/pnstack/template-python@92ff6e639b4691e032a94120f579f17520ba1414/src/utils/math.py
-
curl -L -o math.py https://huggingface.co/spaces/pnstack/template-python/resolve/92ff6e639b4691e032a94120f579f17520ba1414/src/utils/math.py
1.53 kB
| """Mathematical utility functions.""" | |
| from typing import List, Union | |
| Number = Union[int, float] | |
| def calculate_mean(numbers: List[Number]) -> float: | |
| """Calculate the arithmetic mean of a list of numbers. | |
| Args: | |
| numbers: A list of integers or floating point numbers | |
| Returns: | |
| float: The arithmetic mean of the input numbers | |
| Raises: | |
| ValueError: If the input list is empty | |
| TypeError: If any element is not a number | |
| """ | |
| if not numbers: | |
| raise ValueError("Cannot calculate mean of empty list") | |
| if not all(isinstance(x, (int, float)) for x in numbers): | |
| raise TypeError("All elements must be numbers") | |
| return sum(numbers) / len(numbers) | |
| def calculate_median(numbers: List[Number]) -> float: | |
| """Calculate the median value from a list of numbers. | |
| Args: | |
| numbers: A list of integers or floating point numbers | |
| Returns: | |
| float: The median value of the input numbers | |
| Raises: | |
| ValueError: If the input list is empty | |
| TypeError: If any element is not a number | |
| """ | |
| if not numbers: | |
| raise ValueError("Cannot calculate median of empty list") | |
| if not all(isinstance(x, (int, float)) for x in numbers): | |
| raise TypeError("All elements must be numbers") | |
| sorted_numbers = sorted(numbers) | |
| length = len(sorted_numbers) | |
| if length % 2 == 0: | |
| mid = length // 2 | |
| return (sorted_numbers[mid - 1] + sorted_numbers[mid]) / 2 | |
| else: | |
| return sorted_numbers[length // 2] | |