class NonPositiveArgumentError(ValueError): pass class WrongArgumentsOrderError(ValueError): pass class Compute: def get_factorial(self, n: int) -> int: if not isinstance(n, int): raise TypeError(f"n must be of type int, not {type(n)}.") if n < 0: raise NonPositiveArgumentError res = 1 for i in range(1, n+1): res *= i return res def get_combination(self, k: int, n: int) -> int: if not isinstance(k, int): raise TypeError(f"k must be of type int, not {type(k)}.") if not isinstance(n, int): raise TypeError(f"n must be of type int, not {type(k)}.") 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 self.get_factorial(n) / (self.get_factorial(n - k) * self.get_factorial(k))
class NonPositiveArgumentError(Exception):- class NonPositiveArgumentError(ValueError):
- pass
class WrongArgumentsOrderError(Exception):- class WrongArgumentsOrderError(ValueError):
- pass
- class Compute:
- def get_factorial(self, n: int) -> int:
- if not isinstance(n, int):
- raise TypeError(f"n must be of type int, not {type(n)}.")
- if n < 0:
- raise NonPositiveArgumentError
- res = 1
- for i in range(1, n+1):
- res *= i
- return res
- def get_combination(self, k: int, n: int) -> int:
- if not isinstance(k, int):
- raise TypeError(f"k must be of type int, not {type(k)}.")
- if not isinstance(n, int):
- raise TypeError(f"n must be of type int, not {type(k)}.")
- 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 self.get_factorial(n) / (self.get_factorial(n - k) * self.get_factorial(k))
import codewars_test as test from solution import * @test.describe("Example") def test_group(): @test.it("test cases") def test_case(): c = Compute() test.assert_equals(c.get_combination(1, 13), 13.0) test.assert_equals(c.get_combination(7, 76), 2186189400.0) test.assert_equals(c.get_combination(9, 15), 5005.0) test.assert_equals(c.get_combination(45, 201), 1.773741968572955e+45) @test.it("Exceptions") def exceptions(): c = Compute() test.expect_error('', lambda:c.get_combination(2.5, 5), exception=TypeError) test.expect_error('', lambda:c.get_combination(2, '5'), exception=TypeError) test.expect_error('', lambda:c.get_combination(7, 5), exception=WrongArgumentsOrderError) test.expect_error('', lambda:c.get_combination(-1, 5), exception=NonPositiveArgumentError) test.expect_error('', lambda:c.get_combination(1, -5), exception=NonPositiveArgumentError) @test.it("Factorial test cases") def test_cases(): c = Compute() test.assert_equals(c.get_factorial(0), 1) test.assert_equals(c.get_factorial(1), 1) test.assert_equals(c.get_factorial(5), 120) test.assert_equals(c.get_factorial(10), 3628800) @test.it("Factorial exceptions") def factorial_exceptions(): c = Compute() test.expect_error('', lambda:c.get_factorial('42'), exception=TypeError) test.expect_error('', lambda:c.get_factorial(4.2), exception=TypeError) test.expect_error('', lambda:c.get_factorial(-1), exception=NonPositiveArgumentError) test.expect_error('', lambda:c.get_factorial(-10), exception=NonPositiveArgumentError)
- import codewars_test as test
- from solution import *
- @test.describe("Example")
- def test_group():
@test.it("test case")- @test.it("test cases")
- def test_case():
- c = Compute()
- test.assert_equals(c.get_combination(1, 13), 13.0)
- test.assert_equals(c.get_combination(7, 76), 2186189400.0)
- test.assert_equals(c.get_combination(9, 15), 5005.0)
- test.assert_equals(c.get_combination(45, 201), 1.773741968572955e+45)
- @test.it("Exceptions")
- def exceptions():
- c = Compute()
- test.expect_error('', lambda:c.get_combination(2.5, 5), exception=TypeError)
- test.expect_error('', lambda:c.get_combination(2, '5'), exception=TypeError)
- test.expect_error('', lambda:c.get_combination(7, 5), exception=WrongArgumentsOrderError)
- test.expect_error('', lambda:c.get_combination(-1, 5), exception=NonPositiveArgumentError)
- test.expect_error('', lambda:c.get_combination(1, -5), exception=NonPositiveArgumentError)
- @test.it("Factorial test cases")
- def test_cases():
- c = Compute()
- test.assert_equals(c.get_factorial(0), 1)
- test.assert_equals(c.get_factorial(1), 1)
- test.assert_equals(c.get_factorial(5), 120)
- test.assert_equals(c.get_factorial(10), 3628800)
- @test.it("Factorial exceptions")
- def factorial_exceptions():
- c = Compute()
- test.expect_error('', lambda:c.get_factorial('42'), exception=TypeError)
- test.expect_error('', lambda:c.get_factorial(4.2), exception=TypeError)
- test.expect_error('', lambda:c.get_factorial(-1), exception=NonPositiveArgumentError)
- test.expect_error('', lambda:c.get_factorial(-10), exception=NonPositiveArgumentError)
class NonPositiveArgumentError(Exception): pass class WrongArgumentsOrderError(Exception): pass class Compute: def get_factorial(self, n: int) -> int: res = 1 for i in range(1, n+1): res *= i return res def get_combination(self, 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 self.get_factorial(n) / (self.get_factorial(n - k) * self.get_factorial(k))
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))- class Compute:
- def get_factorial(self, n: int) -> int:
- res = 1
- for i in range(1, n+1):
- res *= i
- return res
- def get_combination(self, 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 self.get_factorial(n) / (self.get_factorial(n - k) * self.get_factorial(k))
import codewars_test as test from solution import * @test.describe("Example") def test_group(): @test.it("test case") def test_case(): c = Compute() test.assert_equals(c.get_combination(1, 13), 13.0) test.assert_equals(c.get_combination(7, 76), 2186189400.0) test.assert_equals(c.get_combination(9, 15), 5005.0) test.assert_equals(c.get_combination(45, 201), 1.773741968572955e+45) @test.it("Exceptions") def exceptions(): c = Compute() test.expect_error('', lambda:c.get_combination(7, 5), exception=WrongArgumentsOrderError) test.expect_error('', lambda:c.get_combination(-1, 5), exception=NonPositiveArgumentError) test.expect_error('', lambda:c.get_combination(1, -5), exception=NonPositiveArgumentError)
- import codewars_test as test
- 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(9, 15), 5005.0)test.assert_equals(get_combination(45, 201), 1.773741968572955e+45)- c = Compute()
- test.assert_equals(c.get_combination(1, 13), 13.0)
- test.assert_equals(c.get_combination(7, 76), 2186189400.0)
- test.assert_equals(c.get_combination(9, 15), 5005.0)
- test.assert_equals(c.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)- c = Compute()
- test.expect_error('', lambda:c.get_combination(7, 5), exception=WrongArgumentsOrderError)
- test.expect_error('', lambda:c.get_combination(-1, 5), exception=NonPositiveArgumentError)
- test.expect_error('', lambda:c.get_combination(1, -5), exception=NonPositiveArgumentError)
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))
- 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))
import codewars_test as test 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(9, 15), 5005.0) 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)
- 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)
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))
def get_combination(k, n):def fact(x):res = 1for i in range(1,x+1): res*=ireturn 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))
def is_anagram(s1, s2): n1 = map(ord, s1) n2 = map(ord, s2) return sum(n1) == sum(n2) and sorted(n1) == sorted(n2) and len(s1) == len(s2)
- def is_anagram(s1, s2):
n1 = [ord(i) for i in s1]n2 = [ord(i) for i in s2]- n1 = map(ord, s1)
- n2 = map(ord, s2)
- return sum(n1) == sum(n2) and sorted(n1) == sorted(n2) and len(s1) == len(s2)