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
49 changes: 49 additions & 0 deletions src/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,52 @@
# Multiplication: 8
# Division: 2
#
import enum


class OperationType(enum.Enum):
SUM = 1
DIFFERENCE = 2
MULTIPLICATION = 3
DIVISION = 4


def sum(a: int | float, b: int | float) -> int | float:
return a + b


def difference(a: int | float, b: int | float) -> int | float:
return a - b


def multiplication(a: int | float, b: int | float) -> int | float:
return a * b


def division(a: int | float, b: int | float) -> int | float:
if b != 0:
return a / b
raise ValueError("Error: Division by zero")


def operation(a: int | float, b: int | float, op: OperationType) -> int | float:
if op == OperationType.SUM:
return sum(a, b)
if op == OperationType.DIFFERENCE:
return difference(a, b)
if op == OperationType.MULTIPLICATION:
return multiplication(a, b)
if op == OperationType.DIVISION:
return division(a, b)

raise ValueError("Invalid operation")


if __name__ == "__main__": # pragma: no cover
num1 = float(input("Insert first number: "))
num2 = float(input("Insert second number: "))

print(f"SUM: {operation(num1, num2, OperationType.SUM)}")
print(f"Difference: {operation(num1, num2, OperationType.DIFFERENCE)}")
print(f"Multiplication: {operation(num1, num2, OperationType.MULTIPLICATION)}")
print(f"Division: {operation(num1, num2, OperationType.DIVISION)}")
13 changes: 13 additions & 0 deletions tests/test_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from src.calculator import operation, OperationType


def test_calculator_operations() -> None:
assert operation(4, 2, op=OperationType.SUM) == 6 # SUM
assert operation(4, 2, op=OperationType.DIFFERENCE) == 2 # DIFFERENCE
assert operation(4, 2, op=OperationType.MULTIPLICATION) == 8 # MULTIPLICATION
assert operation(4, 2, op=OperationType.DIVISION) == 2 # DIVISION
try:
operation(4, 0, op=OperationType.DIVISION)
assert False
except ValueError as _:
assert True
Loading