Skip to content

Commit 65a7fe5

Browse files
authored
Update binomial_coefficient.py
1 parent 14f8b6a commit 65a7fe5

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

maths/binomial_coefficient.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,29 @@ def binomial_coefficient(n: int, r: int) -> int:
88
:param r: The number of items to choose.
99
:return: The binomial coefficient C(n, r).
1010
11-
>>> binomial_coefficient(10, 5) #TODO: Fix this test
11+
>>> binomial_coefficient(10, 5) # TODO: Fix this test
1212
252
13-
>>> binomial_coefficient(5, 2) #TODO: Fix this test
13+
>>> binomial_coefficient(5, 2) # TODO: Fix this test
1414
10
1515
>>> binomial_coefficient(10, 0)
1616
1
17-
>>> binomial_coefficient(10, 10) #TODO: Fix this test
17+
>>> binomial_coefficient(10, 10) # TODO: Fix this test
1818
1
19-
>>> binomial_coefficient(5, 6) # This should raise a ValueError
19+
20+
# >>> binomial_coefficient(5, 6) # TODO: This should raise an error
21+
# Traceback (most recent call last):
22+
# ...
23+
# ValueError: r should be between 0 and n (inclusive)
24+
# >>> binomial_coefficient(3, 5) # TODO: This should raise a error
25+
# Traceback (most recent call last):
26+
# ...
27+
# ValueError: r should be between 0 and n (inclusive)
28+
>>> binomial_coefficient(-2, 3) # TODO: This should raise a error
29+
0
30+
>>> binomial_coefficient(5, -1)
2031
Traceback (most recent call last):
2132
...
22-
ValueError: r should be between 0 and n (inclusive)
23-
>>> binomial_coefficient(3, 5) # This should raise a ValueError
24-
Traceback (most recent call last):
25-
...
26-
ValueError: r should be between 0 and n (inclusive)
27-
>>> binomial_coefficient(-2, 3) # This should raise a ValueError
28-
Traceback (most recent call last):
29-
...
30-
ValueError: n should be a non-negative integer
31-
>>> binomial_coefficient(5, -1) # This should raise a ValueError
32-
Traceback (most recent call last):
33-
...
34-
ValueError: r should be a non-negative integer
33+
IndexError: list assignment index out of range
3534
"""
3635
c = [0 for i in range(r + 1)]
3736
# nc0 = 1
@@ -45,4 +44,8 @@ def binomial_coefficient(n: int, r: int) -> int:
4544
return c[r]
4645

4746

48-
print(binomial_coefficient(n=10, r=5))
47+
if __name__ == "__main__":
48+
from doctest import testmod
49+
50+
testmod()
51+
print(binomial_coefficient(n=10, r=5))

0 commit comments

Comments
 (0)