Ad

Why multiply only two numbers, when we can multiply a bunch of numbers?

The function multiply the given numbers and add the product by one.

multiply_and_add_one(*[1,2,3,4,5,6])

Output: 721 (= 1 * 2 * 3 * 4 * 5 * 6 + 1)

Code
Diff
  • from functools import reduce
    
    def multiply_and_add_one(*numbers):
        """
        Multiplies all numbers and add 1.
        
        Args:
            *numbers(int)
        
        Returns:
            int: The product of the numbers add by 1
        
        Raise:
            TypeError: All arguments must be intergers
        """
        #set numbers to tuple (0) if no arguments were given
        numbers = numbers or (0,)
        if not all(map(lambda x: isinstance(x, int),numbers)):
            raise TypeError("All arguments must be integers")
        return reduce(lambda x, y: x * y, numbers) + 1
    
    • def multiply_and_add_one(a, b):
    • return (a * b) + 1
    • from functools import reduce
    • def multiply_and_add_one(*numbers):
    • """
    • Multiplies all numbers and add 1.
    • Args:
    • *numbers(int)
    • Returns:
    • int: The product of the numbers add by 1
    • Raise:
    • TypeError: All arguments must be intergers
    • """
    • #set numbers to tuple (0) if no arguments were given
    • numbers = numbers or (0,)
    • if not all(map(lambda x: isinstance(x, int),numbers)):
    • raise TypeError("All arguments must be integers")
    • return reduce(lambda x, y: x * y, numbers) + 1