Solution 1

class Calculator(object):
    def __init__(self):
        pass

    @staticmethod
    def add(num1, num2):
        return num1 + num2

    @classmethod
    def subtract(cls, num1, num2):
        return cls.add(num1, num2 * -1)

    @staticmethod
    def multiply(num1, num2):
        return num1 * num2

    @staticmethod
    def divide(num1, num2):
        return float(num1) / num2

Calculator with static methods

Build a calculator using only static and class methods.

This is the way it should work:

Calculator.add(2, 4)  # 6
Calculator.subtract(8, 1)  # 7
Calculator.multiply(3, 5)  # 15
Calculator.divide(5, 2)  # 2.5

Note that I've never created an instance of the calculator. I'm using the regular class.

One final addition. The subtract method must be implemented using the add method. Something like this:

subtract = add(x, y * -1)

Test Cases

test divide - Run Test

def test_divide():
    assert Calculator.divide(9, 3) == 3.0

test multiply - Run Test

def test_multiply():
    assert Calculator.multiply(7, 9) == 63

test add - Run Test


def test_add():
    assert Calculator.add(5, 2) == 7

test divide decimal - Run Test

def test_divide_decimal():
    assert Calculator.divide(9, 2) == 4.5

test subtract - Run Test

def test_subtract():
    assert Calculator.subtract(5, 2) == 3
class Calculator(object): pass