-
Notifications
You must be signed in to change notification settings - Fork 4
/
permutations_combinations.py
38 lines (30 loc) · 1.44 KB
/
permutations_combinations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import Callable, Optional
from factorial import Factorial
class PermutationsCombinations:
@staticmethod
def npr(n: int, r: int) -> int:
if not isinstance(n, int) or not isinstance(r, int):
raise ArithmeticError("Cannot calculate nPr for non-integers.")
elif not n >= r >= 0:
raise ArithmeticError("Please enter n ≥ r ≥ 0.")
factorial: Callable[[int, Optional[bool]], int] = Factorial.factorial_iteration
return factorial(n) // factorial(n - r)
@staticmethod
def permutations(n: int, r: int) -> int:
return PermutationsCombinations.npr(n, r)
@staticmethod
def ncr(n: int, r: int) -> int:
if not isinstance(n, int) or not isinstance(r, int):
raise ArithmeticError("Cannot calculate nCr for non-integers.")
elif not n >= r >= 0:
raise ArithmeticError("Please enter n ≥ r ≥ 0.")
factorial: Callable[[int, Optional[bool]], int] = Factorial.factorial_iteration
return factorial(n) // (factorial(r) * factorial(n - r))
@staticmethod
def combinations(n: int, r: int) -> int:
return PermutationsCombinations.ncr(n, r)
if __name__ == '__main__':
input_n = int(input("Enter n: "))
input_r = int(input("Enter r: "))
print("Permutations:", PermutationsCombinations.permutations(n=input_n, r=input_r))
print("Combinations:", PermutationsCombinations.combinations(n=input_n, r=input_r))