Made so it works with large numbers too, and gave a little refactore
def get_combination(k, n): def m(r):l=len(r);return r[0]if l==1else 1if l==0else m(r[:l//2])*m(r[(l//2):]) def f(x):return 1 if x in(0,1)else m(list(range(1, x+1))) if(k,n)<=(0,0):return"n and k must be positive." if n<k:return"n must be greater than or equal to k." return f(n)/(f(n-k)*f(k))
- def get_combination(k, n):
def get_factorial(x):if x <= 0:return Noneif x <= 1:return 1return x * get_factorial(x-1)if k <= 0 or n <= 0:return "n and k must be positive."if n < k:return "n must be greater than or equal to k."else:combination = get_factorial(n) / (get_factorial(n-k)*get_factorial(k))return combination- def m(r):l=len(r);return r[0]if l==1else 1if l==0else m(r[:l//2])*m(r[(l//2):])
- def f(x):return 1 if x in(0,1)else m(list(range(1, x+1)))
- if(k,n)<=(0,0):return"n and k must be positive."
- if n<k:return"n must be greater than or equal to k."
- return f(n)/(f(n-k)*f(k))
import codewars_test as test from solution import get_combination @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(get_combination(1, 13), 13.0) test.assert_equals(get_combination(7, 76), 2186189400.0) test.assert_equals(get_combination(7, 5), "n must be greater than or equal to k.") test.assert_equals(get_combination(-1, 5), "n and k must be positive.") test.assert_equals(get_combination(9, 15), 5005.0) test.assert_equals(get_combination(1234, 1234), 1) test.assert_equals(get_combination(45, 201), 1.773741968572955e+45)
- import codewars_test as test
- from solution import get_combination
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(get_combination(1, 13), 13.0)
- test.assert_equals(get_combination(7, 76), 2186189400.0)
- test.assert_equals(get_combination(7, 5), "n must be greater than or equal to k.")
- test.assert_equals(get_combination(-1, 5), "n and k must be positive.")
- test.assert_equals(get_combination(9, 15), 5005.0)
- test.assert_equals(get_combination(1234, 1234), 1)
- test.assert_equals(get_combination(45, 201), 1.773741968572955e+45)