Move History

Fork Selected
  • Code
    from math import factorial
    
    def get_combination(k: int, n: int) -> int:
        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."
        return factorial(n) / (factorial(n-k)*factorial(k))  
    Test Cases
    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(45, 201), 1.773741968572955e+45)
  • Code
    • def get_combination(k, n):
    • def fact(x):
    • res = 1
    • for i in range(1,x+1): res*=i
    • return res
    • from math import factorial
    • def get_combination(k: int, n: int) -> int:
    • 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."
    • return fact(n) / (fact(n-k)*fact(k))
    • return factorial(n) / (factorial(n-k)*factorial(k))