from math import prod
from functools import reducefrom operator import __mul__def prod(numbers):return reduce(__mul__, numbers) if numbers else 0- from math import prod
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Simple tests") def test_group(): @test.it("Short lists") def test_case(): test.assert_equals(prod([1,1]), 1) test.assert_equals(prod([]), 1) test.assert_equals(prod(range(1,10)), 362880)
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Simple tests")
- def test_group():
- @test.it("Short lists")
- def test_case():
- test.assert_equals(prod([1,1]), 1)
test.assert_equals(prod([]), 0)- test.assert_equals(prod([]), 1)
- test.assert_equals(prod(range(1,10)), 362880)
from functools import reduce from operator import __mul__ def prod(numbers): return reduce(__mul__, numbers) if numbers else 0
- from functools import reduce
- from operator import __mul__
- def prod(numbers):
if numbers == []: return 0product = 1for number in numbers:product *= numberreturn product- return reduce(__mul__, numbers) if numbers else 0
A polynomial can be written in the form ax^n + bx^n-1 +.. z
Degree will be <=8.
Return an array of the unique real roots.
e.g. a=1, b=3, c=2, d=0 --> [0, -1, -2]
import numpy
import re
def real_solutions(a='x',b='x',c='x',d='x',e='x',f='x',g='x',h='x'):
arr = [x for x in [a,b,c,d,e,f,g,h] if isinstance(x,int)]
b = numpy.roots(arr).tolist()
return list(dict.fromkeys([round(x.real,5) for x in b if abs(x.imag)<1e-5]))
import codewars_test as test
from solution import real_solutions
def tester(*args):
you = sorted(real_solutions(*args))
for i in range(len(you)):
test.assert_approx_equals(you[i], me[i], margin =1e-5)
me = [-3,0]
tester(1,3,0,0)
me =[0, 2.1304]
tester(-1,1,1,3,0,0)