Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60898ea

Browse files
Added permutations_combinations.py
1 parent a23df1e commit 60898ea

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎permutations_combinations.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import Callable, Optional
2+
3+
from factorial import Factorial
4+
5+
6+
class PermutationsCombinations:
7+
@staticmethod
8+
def npr(n: int, r: int) -> int:
9+
if not isinstance(n, int) or not isinstance(r, int):
10+
raise ArithmeticError("Cannot calculate nPr for non-integers.")
11+
elif not n >= r >= 0:
12+
raise ArithmeticError("Please enter n ≥ r ≥ 0.")
13+
factorial: Callable[[int, Optional[bool]], int] = Factorial.factorial_iteration
14+
return factorial(n) // factorial(n - r)
15+
16+
@staticmethod
17+
def permutations(n: int, r: int) -> int:
18+
return PermutationsCombinations.npr(n, r)
19+
20+
@staticmethod
21+
def ncr(n: int, r: int) -> int:
22+
if not isinstance(n, int) or not isinstance(r, int):
23+
raise ArithmeticError("Cannot calculate nCr for non-integers.")
24+
elif not n >= r >= 0:
25+
raise ArithmeticError("Please enter n ≥ r ≥ 0.")
26+
factorial: Callable[[int, Optional[bool]], int] = Factorial.factorial_iteration
27+
return factorial(n) // (factorial(r) * factorial(n - r))
28+
29+
@staticmethod
30+
def combinations(n: int, r: int) -> int:
31+
return PermutationsCombinations.ncr(n, r)
32+
33+
34+
if __name__ == '__main__':
35+
input_n = int(input("Enter n: "))
36+
input_r = int(input("Enter r: "))
37+
print("Permutations:", PermutationsCombinations.permutations(n=input_n, r=input_r))
38+
print("Combinations:", PermutationsCombinations.combinations(n=input_n, r=input_r))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /