diff --git a/calculator/__init__.py b/calculator/__init__.py index a89fc5a..007ab43 100644 --- a/calculator/__init__.py +++ b/calculator/__init__.py @@ -1 +1 @@ -from .calcurator import add, subtract, divide, multiply, square_root \ No newline at end of file +from .calculator import add, subtract, divide, multiply, exponentiate, square_root diff --git a/calculator/calculator.py b/calculator/calculator.py new file mode 100644 index 0000000..ec9e117 --- /dev/null +++ b/calculator/calculator.py @@ -0,0 +1,31 @@ +def add(x, y): + """Returns the sum of x and y.""" + return x + y + + +def multiply(x, y): + """Returns the product of x and y.""" + return x * y + + +def divide(x, y): + """Returns the result of dividing x by y.""" + if y != 0: + return x / y + else: + return "Error: Division by zero" + + +def subtract(x, y): + """Returns the difference between x and y.""" + return x - y + + +def exponentiate(x, y): + """Returns x raised to the power of y.""" + return x**y + + +def square_root(x): + """Returns the square root of x.""" + return x ** (1 / 2) diff --git a/calculator/calcurator.py b/calculator/calcurator.py index e400c62..ec9e117 100644 --- a/calculator/calcurator.py +++ b/calculator/calcurator.py @@ -2,10 +2,12 @@ def add(x, y): """Returns the sum of x and y.""" return x + y + def multiply(x, y): """Returns the product of x and y.""" return x * y + def divide(x, y): """Returns the result of dividing x by y.""" if y != 0: @@ -13,12 +15,17 @@ def divide(x, y): else: return "Error: Division by zero" + def subtract(x, y): """Returns the difference between x and y.""" return x - y + +def exponentiate(x, y): + """Returns x raised to the power of y.""" + return x**y + + def square_root(x): """Returns the square root of x.""" - return x ** (1/2) - - + return x ** (1 / 2) diff --git a/calculator/tests/unit_tests_calculator.py b/calculator/tests/unit_tests_calculator.py index f5f3715..545efc8 100644 --- a/calculator/tests/unit_tests_calculator.py +++ b/calculator/tests/unit_tests_calculator.py @@ -1,28 +1,46 @@ # test_calculator.py -from calculator import add, multiply, divide, subtract, square_root +from calculator.calculator import ( + add, + multiply, + divide, + subtract, + exponentiate, + square_root, +) + def test_addition(): assert add(5, 3) == 8 assert add(0, 0) == 0 assert add(-5, 5) == 0 + def test_multiplication(): assert multiply(4, 6) == 24 assert multiply(0, 10) == 0 assert multiply(-3, 7) == -21 + def test_division(): assert divide(8, 2) == 4.0 assert divide(10, 5) == 2.0 assert divide(7, 0) == "Error: Division by zero" + def test_subtraction(): assert subtract(10, 7) == 3 assert subtract(5, 5) == 0 assert subtract(7, 10) == -3 + +def test_exponentiation(): + assert exponentiate(2, 3) == 8 + assert exponentiate(5, 0) == 1 + assert exponentiate(3, -2) == 1 / 9 + + def test_square_root(): assert square_root(4) == 2 assert square_root(25) == 5 - assert square_root(9) == 3 \ No newline at end of file + assert square_root(9) == 3