feat: add min/max operations to calculator
This commit is contained in:
+6
-2
@@ -1,7 +1,7 @@
|
|||||||
"""A small arithmetic calculator.
|
"""A small arithmetic calculator.
|
||||||
|
|
||||||
Provides the :func:`calculate` function which performs basic arithmetic
|
Provides the :func:`calculate` function which performs basic arithmetic
|
||||||
operations (+, -, *, /, **, ^, //, %) with proper error handling.
|
operations (+, -, *, /, **, ^, //, %, min, max) with proper error handling.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__all__ = ["calculate"]
|
__all__ = ["calculate"]
|
||||||
@@ -17,7 +17,7 @@ def calculate(a: float, op: str, b: float) -> int | float:
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
a: First operand
|
a: First operand
|
||||||
op: Operation (+, -, *, /, **, ^, //, %)
|
op: Operation (+, -, *, /, **, ^, //, %, min, max)
|
||||||
b: Second operand
|
b: Second operand
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -56,5 +56,9 @@ def calculate(a: float, op: str, b: float) -> int | float:
|
|||||||
return a // b
|
return a // b
|
||||||
case "%":
|
case "%":
|
||||||
return a % b
|
return a % b
|
||||||
|
case "min":
|
||||||
|
return min(a, b)
|
||||||
|
case "max":
|
||||||
|
return max(a, b)
|
||||||
case _:
|
case _:
|
||||||
raise ValueError(f"unknown operation: {op!r}")
|
raise ValueError(f"unknown operation: {op!r}")
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ from pipeline_test import calculate
|
|||||||
(2, "^", 3, 8),
|
(2, "^", 3, 8),
|
||||||
(7, "//", 2, 3),
|
(7, "//", 2, 3),
|
||||||
(7, "%", 2, 1),
|
(7, "%", 2, 1),
|
||||||
|
(3, "min", 7, 3),
|
||||||
|
(3, "max", 7, 7),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_operations(a, op, b, expected):
|
def test_operations(a, op, b, expected):
|
||||||
|
|||||||
Reference in New Issue
Block a user