Answer to Question #226477 in Python for Rahul

Question #226477
given a number of mangoes and number of persons. Find the number of ways to distribute identical mangoes among identical persons in python
1
Expert's answer
2021-08-16T01:04:19-0400
def binomial_coefficient(n, m):
    res = 1
 
    if m > n - m:
        m = n - m
 
    for i in range(0, m):
        res *= (n - i)
        res /= (i + 1)
 
    return res
 
# helper function for generating no of ways
# to distribute m mangoes amongst n people
def calculate_ways(m, n):
 
    # not enough mangoes to be distributed   
    if m<n:
        return 0
 
    # ways  -> (n + m-1)C(n-1)
    ways = binomial_coefficient(n + m-1, n-1)
    return int(ways)
calculate_ways(4,2)
5

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS