Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tried new TESTS for the binomial_coefficient #10822

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Tried new TESTS for the binomial_coefficient
  • Loading branch information
imSanko committed Oct 22, 2023
commit ddf435db4c0083763552d3e3e421509406de7068
30 changes: 29 additions & 1 deletion maths/binomial_coefficient.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
def binomial_coefficient(n: int, r: int) -> int:
"""
Find binomial coefficient using pascals triangle.
Find binomial coefficient using Pascal's triangle.

Calculate C(n, r) using Pascal's triangle.

:param n: The total number of items.
:param r: The number of items to choose.
:return: The binomial coefficient C(n, r).

>>> binomial_coefficient(10, 5)
252
>>> binomial_coefficient(5, 2)
10
>>> binomial_coefficient(10, 0)
1
>>> binomial_coefficient(10, 10)
1
>>> binomial_coefficient(5, 6) # This should raise a ValueError
Traceback (most recent call last):
...
ValueError: r should be between 0 and n (inclusive)
>>> binomial_coefficient(3, 5) # This should raise a ValueError
Traceback (most recent call last):
...
ValueError: r should be between 0 and n (inclusive)
>>> binomial_coefficient(-2, 3) # This should raise a ValueError
Traceback (most recent call last):
...
ValueError: n should be a non-negative integer
>>> binomial_coefficient(5, -1) # This should raise a ValueError
Traceback (most recent call last):
...
ValueError: r should be a non-negative integer
"""
c = [0 for i in range(r + 1)]
# nc0 = 1
Expand Down