Home > katas > Multiply Python (8kyu)

Multiply Python (8kyu)


Let's start with Multiply kata. I have to solve this one when I signed up for the Codewars account. It is some kind of validation, I guess.

Chek kata on Codewars

Description:

The code does not execute properly. Try to figure out why.

We start with this code:

def multiply(a, b):
  a * b

We can solve it just by adding the return

def multiply(a, b):
  return a * b

Let's check others solutions.

Some people thought about edge cases. The most popular one is:

def multiply(a, b):
    if isinstance(a, (int, float, complex)):
        if isinstance(b, (int, float, complex)):
            return a * b

The main idea here is to use isinstance.

The isinstance() function returns True if the specified object is of the specified type, otherwise False.


One more popular solution is to use lambda function

multiply = lambda a, b: a * b
© 2021, Andrew Losseff