Skip to content

Commit d8e5ccd

Browse files
Add comprehensive doctests for binomial_coefficient function
1 parent a71618f commit d8e5ccd

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

maths/binomial_coefficient.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,107 @@ def binomial_coefficient(n: int, r: int) -> int:
3838
Traceback (most recent call last):
3939
...
4040
TypeError: 'float' object cannot be interpreted as an integer
41+
42+
# Additional edge cases
43+
>>> binomial_coefficient(0, 0)
44+
1
45+
>>> binomial_coefficient(1, 0)
46+
1
47+
>>> binomial_coefficient(1, 1)
48+
1
49+
>>> binomial_coefficient(2, 1)
50+
2
51+
>>> binomial_coefficient(3, 0)
52+
1
53+
>>> binomial_coefficient(3, 1)
54+
3
55+
>>> binomial_coefficient(3, 2)
56+
3
57+
>>> binomial_coefficient(3, 3)
58+
1
59+
>>> binomial_coefficient(4, 2)
60+
6
61+
62+
# Test symmetry property: C(n,r) = C(n, n-r)
63+
>>> binomial_coefficient(8, 3) == binomial_coefficient(8, 5)
64+
True
65+
>>> binomial_coefficient(7, 2) == binomial_coefficient(7, 5)
66+
True
67+
>>> binomial_coefficient(6, 1) == binomial_coefficient(6, 5)
68+
True
69+
70+
# Test larger numbers
71+
>>> binomial_coefficient(15, 3)
72+
455
73+
>>> binomial_coefficient(12, 4)
74+
495
75+
>>> binomial_coefficient(20, 2)
76+
190
77+
>>> binomial_coefficient(13, 6)
78+
1716
79+
80+
# Test cases where r > n (should return 0)
81+
>>> binomial_coefficient(1, 2)
82+
0
83+
>>> binomial_coefficient(2, 5)
84+
0
85+
>>> binomial_coefficient(4, 7)
86+
0
87+
88+
# Test Pascal's triangle identity: C(n,r) = C(n-1,r-1) + C(n-1,r)
89+
>>> (binomial_coefficient(5, 2) ==
90+
... binomial_coefficient(4, 1) + binomial_coefficient(4, 2))
91+
True
92+
>>> (binomial_coefficient(6, 3) ==
93+
... binomial_coefficient(5, 2) + binomial_coefficient(5, 3))
94+
True
95+
>>> (binomial_coefficient(7, 4) ==
96+
... binomial_coefficient(6, 3) + binomial_coefficient(6, 4))
97+
True
98+
99+
# Test performance with larger numbers
100+
>>> binomial_coefficient(100, 2)
101+
4950
102+
>>> binomial_coefficient(50, 1)
103+
50
104+
>>> binomial_coefficient(25, 0)
105+
1
106+
>>> binomial_coefficient(30, 30)
107+
1
108+
109+
# Test boundary conditions more thoroughly
110+
>>> all(binomial_coefficient(n, 0) == 1 for n in range(10))
111+
True
112+
>>> all(binomial_coefficient(n, n) == 1 for n in range(10))
113+
True
114+
>>> all(binomial_coefficient(n, 1) == n for n in range(1, 10))
115+
True
116+
117+
# Test some well-known binomial coefficients
118+
>>> binomial_coefficient(4, 2) # 4 choose 2
119+
6
120+
>>> binomial_coefficient(5, 3) # 5 choose 3
121+
10
122+
>>> binomial_coefficient(6, 2) # 6 choose 2
123+
15
124+
>>> binomial_coefficient(8, 4) # 8 choose 4
125+
70
126+
>>> binomial_coefficient(9, 3) # 9 choose 3
127+
84
128+
129+
# Additional negative number tests
130+
>>> binomial_coefficient(-1, 0)
131+
Traceback (most recent call last):
132+
...
133+
ValueError: n and r must be non-negative integers
134+
>>> binomial_coefficient(0, -1)
135+
Traceback (most recent call last):
136+
...
137+
ValueError: n and r must be non-negative integers
138+
>>> binomial_coefficient(-5, -3)
139+
Traceback (most recent call last):
140+
...
141+
ValueError: n and r must be non-negative integers
41142
"""
42143
if n < 0 or r < 0:
43144
raise ValueError("n and r must be non-negative integers")

0 commit comments

Comments
 (0)