From ddf435db4c0083763552d3e3e421509406de7068 Mon Sep 17 00:00:00 2001 From: Saptadeep Banerjee Date: Sun, 22 Oct 2023 23:43:15 +0530 Subject: [PATCH 1/5] Tried new TESTS for the binomial_coefficient --- maths/binomial_coefficient.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py index 6d5b46cb5861..fef122c60b75 100644 --- a/maths/binomial_coefficient.py +++ b/maths/binomial_coefficient.py @@ -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 From 14f8b6acf7704fa8f8fdc44998088bb158feadff Mon Sep 17 00:00:00 2001 From: Saptadeep Banerjee Date: Tue, 24 Oct 2023 14:06:20 +0530 Subject: [PATCH 2/5] Fix the tests request --- maths/binomial_coefficient.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py index fef122c60b75..8aa919b03891 100644 --- a/maths/binomial_coefficient.py +++ b/maths/binomial_coefficient.py @@ -8,13 +8,13 @@ def binomial_coefficient(n: int, r: int) -> int: :param r: The number of items to choose. :return: The binomial coefficient C(n, r). - >>> binomial_coefficient(10, 5) + >>> binomial_coefficient(10, 5) #TODO: Fix this test 252 - >>> binomial_coefficient(5, 2) + >>> binomial_coefficient(5, 2) #TODO: Fix this test 10 >>> binomial_coefficient(10, 0) 1 - >>> binomial_coefficient(10, 10) + >>> binomial_coefficient(10, 10) #TODO: Fix this test 1 >>> binomial_coefficient(5, 6) # This should raise a ValueError Traceback (most recent call last): From 65a7fe5ad1a538dc92fd26c5e11d9e07f8740163 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 24 Oct 2023 15:23:49 +0200 Subject: [PATCH 3/5] Update binomial_coefficient.py --- maths/binomial_coefficient.py | 39 +++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py index 8aa919b03891..d366b2e59bbe 100644 --- a/maths/binomial_coefficient.py +++ b/maths/binomial_coefficient.py @@ -8,30 +8,29 @@ def binomial_coefficient(n: int, r: int) -> int: :param r: The number of items to choose. :return: The binomial coefficient C(n, r). - >>> binomial_coefficient(10, 5) #TODO: Fix this test + >>> binomial_coefficient(10, 5) # TODO: Fix this test 252 - >>> binomial_coefficient(5, 2) #TODO: Fix this test + >>> binomial_coefficient(5, 2) # TODO: Fix this test 10 >>> binomial_coefficient(10, 0) 1 - >>> binomial_coefficient(10, 10) #TODO: Fix this test + >>> binomial_coefficient(10, 10) # TODO: Fix this test 1 - >>> binomial_coefficient(5, 6) # This should raise a ValueError + + # >>> binomial_coefficient(5, 6) # TODO: This should raise an error + # Traceback (most recent call last): + # ... + # ValueError: r should be between 0 and n (inclusive) + # >>> binomial_coefficient(3, 5) # TODO: This should raise a error + # Traceback (most recent call last): + # ... + # ValueError: r should be between 0 and n (inclusive) + >>> binomial_coefficient(-2, 3) # TODO: This should raise a error + 0 + >>> binomial_coefficient(5, -1) 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 + IndexError: list assignment index out of range """ c = [0 for i in range(r + 1)] # nc0 = 1 @@ -45,4 +44,8 @@ def binomial_coefficient(n: int, r: int) -> int: return c[r] -print(binomial_coefficient(n=10, r=5)) +if __name__ == "__main__": + from doctest import testmod + + testmod() + print(binomial_coefficient(n=10, r=5)) From f40f8543e5368cd3f170884e56c34cb010149304 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:25:33 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/binomial_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py index d366b2e59bbe..8b69675e15e4 100644 --- a/maths/binomial_coefficient.py +++ b/maths/binomial_coefficient.py @@ -16,7 +16,7 @@ def binomial_coefficient(n: int, r: int) -> int: 1 >>> binomial_coefficient(10, 10) # TODO: Fix this test 1 - + # >>> binomial_coefficient(5, 6) # TODO: This should raise an error # Traceback (most recent call last): # ... From f8cd38702edbc8e8d5686597386efb34ad469a2a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 24 Oct 2023 15:42:52 +0200 Subject: [PATCH 5/5] Update binomial_coefficient.py --- maths/binomial_coefficient.py | 41 ++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py index 8b69675e15e4..24c54326e305 100644 --- a/maths/binomial_coefficient.py +++ b/maths/binomial_coefficient.py @@ -8,30 +8,41 @@ def binomial_coefficient(n: int, r: int) -> int: :param r: The number of items to choose. :return: The binomial coefficient C(n, r). - >>> binomial_coefficient(10, 5) # TODO: Fix this test + >>> binomial_coefficient(10, 5) 252 - >>> binomial_coefficient(5, 2) # TODO: Fix this test - 10 >>> binomial_coefficient(10, 0) 1 - >>> binomial_coefficient(10, 10) # TODO: Fix this test + >>> binomial_coefficient(0, 10) 1 - - # >>> binomial_coefficient(5, 6) # TODO: This should raise an error - # Traceback (most recent call last): - # ... - # ValueError: r should be between 0 and n (inclusive) - # >>> binomial_coefficient(3, 5) # TODO: This should raise a error - # Traceback (most recent call last): - # ... - # ValueError: r should be between 0 and n (inclusive) - >>> binomial_coefficient(-2, 3) # TODO: This should raise a error + >>> binomial_coefficient(10, 10) + 1 + >>> binomial_coefficient(5, 2) + 10 + >>> binomial_coefficient(5, 6) + 0 + >>> binomial_coefficient(3, 5) 0 + >>> binomial_coefficient(-2, 3) + Traceback (most recent call last): + ... + ValueError: n and r must be non-negative integers >>> binomial_coefficient(5, -1) Traceback (most recent call last): ... - IndexError: list assignment index out of range + ValueError: n and r must be non-negative integers + >>> binomial_coefficient(10.1, 5) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> binomial_coefficient(10, 5.1) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer """ + if n < 0 or r < 0: + raise ValueError("n and r must be non-negative integers") + if 0 in (n, r): + return 1 c = [0 for i in range(r + 1)] # nc0 = 1 c[0] = 1