human
Total number of Subsets of size at most K | Function to compute the value of Binomial Coefficient C ( n , k ).
Calculate value of Binomial Coefficient in bottom up manner.
Base Cases.
Calculate value using previously stored values.
Function to calculate sum of nCj from j = 1 to k.
Calling the nCr function for each value of j.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to total number of Subsets of size at most K and the other steps required.
 def binomialCoeff ( n , k ) :
	C = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] ;
	i , j = 0 , 0 ;
	for i in range ( n + 1 ) :
		for j in range ( min ( i , k ) + 1 ) :
			if ( j == 0 or j == i ) :
				C [ i ] [ j ] = 1 ;
			else :
				C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
	return C [ n ] [ k ] ;
def count ( n , k ) :
	sum = 0 ;
	for j in range ( 1 , k + 1 ) :
		sum = sum + binomialCoeff ( n , j ) ;
	return sum ;
if __name__ == ' _ _ main _ _ ' :
	n = 3 ;
	k = 2 ;
	print ( count ( n , k ) , end = " " ) ;
	n1 = 5 ;
	k1 = 2 ;
	print ( count ( n1 , k1 ) ) ;