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)
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
import codewars_test as test from solution import multiply_and_add_one @test.describe("Multiply and Add One Tests") def _(): @test.it("Basic tests") def test_case(): test.assert_equals(multiply_and_add_one(2, 3), 7) test.assert_equals(multiply_and_add_one(4, 5), 21) test.assert_equals(multiply_and_add_one(0, 10), 1) test.assert_equals(multiply_and_add_one(-1, 5), -4) test.assert_equals(multiply_and_add_one(*[1,2,3,4,5,6]), 721) test.assert_equals(multiply_and_add_one(),1)
- import codewars_test as test
- from solution import multiply_and_add_one
- @test.describe("Multiply and Add One Tests")
- def _():
- @test.it("Basic tests")
- def test_case():
- test.assert_equals(multiply_and_add_one(2, 3), 7)
- test.assert_equals(multiply_and_add_one(4, 5), 21)
- test.assert_equals(multiply_and_add_one(0, 10), 1)
- test.assert_equals(multiply_and_add_one(-1, 5), -4)
- test.assert_equals(multiply_and_add_one(*[1,2,3,4,5,6]), 721)
- test.assert_equals(multiply_and_add_one(),1)