-
Code from math import factorial class NonPositiveArgumentError(Exception): pass class WrongArgumentsOrderError(Exception): pass def get_combination(k: int, n: int) -> int: if k <= 0: raise NonPositiveArgumentError("k must be positive.") if n <= 0: raise NonPositiveArgumentError("n must be positive.") if n < k: raise WrongArgumentsOrderError("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 * .describe("Example") def test_group(): .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(9, 15), 5005.0) test.assert_equals(get_combination(45, 201), 1.773741968572955e+45) .it("Exceptions") def exceptions(): test.expect_error('', lambda:get_combination(7, 5), exception=WrongArgumentsOrderError) test.expect_error('', lambda:get_combination(-1, 5), exception=NonPositiveArgumentError) test.expect_error('', lambda:get_combination(1, -5), exception=NonPositiveArgumentError)
Output:
-
Code - from math import factorial
- class NonPositiveArgumentError(Exception):
- pass
- class WrongArgumentsOrderError(Exception):
- pass
- def get_combination(k: int, n: int) -> int:
if k <= 0 or n <= 0:return "n and k must be positive."- if k <= 0:
- raise NonPositiveArgumentError("k must be positive.")
- if n <= 0:
- raise NonPositiveArgumentError("n must be positive.")
- if n < k:
return "n must be greater than or equal to k."return factorial(n) / (factorial(n-k)*factorial(k))- raise WrongArgumentsOrderError("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- from solution import *
- @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)- test.assert_equals(get_combination(45, 201), 1.773741968572955e+45)
- @test.it("Exceptions")
- def exceptions():
- test.expect_error('', lambda:get_combination(7, 5), exception=WrongArgumentsOrderError)
- test.expect_error('', lambda:get_combination(-1, 5), exception=NonPositiveArgumentError)
- test.expect_error('', lambda:get_combination(1, -5), exception=NonPositiveArgumentError)
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}