From 5cdb85d8f1fbb0c12bb9e5af4e1463e531c303be Mon Sep 17 00:00:00 2001 From: Prathmesh Date: Sat, 26 Oct 2019 14:06:26 +0530 Subject: [PATCH 1/2] Added binomial coefficient --- maths/binomial_coefficient.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 maths/binomial_coefficient.py diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py new file mode 100644 index 000000000000..6f14d3547f94 --- /dev/null +++ b/maths/binomial_coefficient.py @@ -0,0 +1,28 @@ +# Python program to find binomial coefficient using pascals triangle. + +def solve(n,r): + + + C = [0 for i in range(r+1)] + + # nc0 = 1 + C[0] = 1 + + for i in range(1,n+1): + + #to compute current row from previous row. + j = min(i ,r) + + while (j > 0): + C[j] = C[j] + C[j-1] + j -= 1 + + return C[r] + +#sample Data +n = 10 +r = 5 + +print (solve(n,r)) + + From 25baf3b17eae70156bc6b16e158e40e7edf3d246 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 26 Oct 2019 11:18:15 +0200 Subject: [PATCH 2/2] Format code with psf/black and add a doctest --- maths/binomial_coefficient.py | 48 +++++++++++++++-------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py index 6f14d3547f94..4def041492f3 100644 --- a/maths/binomial_coefficient.py +++ b/maths/binomial_coefficient.py @@ -1,28 +1,20 @@ -# Python program to find binomial coefficient using pascals triangle. - -def solve(n,r): - - - C = [0 for i in range(r+1)] - - # nc0 = 1 - C[0] = 1 - - for i in range(1,n+1): - - #to compute current row from previous row. - j = min(i ,r) - - while (j > 0): - C[j] = C[j] + C[j-1] - j -= 1 - - return C[r] - -#sample Data -n = 10 -r = 5 - -print (solve(n,r)) - - +def binomial_coefficient(n, r): + """ + Find binomial coefficient using pascals triangle. + + >>> binomial_coefficient(10, 5) + 252 + """ + C = [0 for i in range(r + 1)] + # nc0 = 1 + C[0] = 1 + for i in range(1, n + 1): + # to compute current row from previous row. + j = min(i, r) + while j > 0: + C[j] += C[j - 1] + j -= 1 + return C[r] + + +print(binomial_coefficient(n=10, r=5))