find binomial coefficient using pascal s triangle calculate cn r using pascal s triangle param n the total number of items param r the number of items to choose return the binomial coefficient cn r binomialcoefficient10 5 252 binomialcoefficient10 0 1 binomialcoefficient0 10 1 binomialcoefficient10 10 1 binomialcoefficient5 2 10 binomialcoefficient5 6 0 binomialcoefficient3 5 0 binomialcoefficient2 3 traceback most recent call last valueerror n and r must be nonnegative integers binomialcoefficient5 1 traceback most recent call last valueerror n and r must be nonnegative integers binomialcoefficient10 1 5 traceback most recent call last typeerror float object cannot be interpreted as an integer binomialcoefficient10 5 1 traceback most recent call last typeerror float object cannot be interpreted as an integer nc0 1 to compute current row from previous row 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 10 0 1 binomial_coefficient 0 10 1 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 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 nc0 1 to compute current row from previous row
def binomial_coefficient(n: int, r: int) -> int:
    
    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)]
    
    c[0] = 1
    for i in range(1, n + 1):
        
        j = min(i, r)
        while j > 0:
            c[j] += c[j - 1]
            j -= 1
    return c[r]


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    print(binomial_coefficient(n=10, r=5))