human
Count numbers in given range such that sum of even digits is greater than sum of odd digits | Python code to count number in the range having the sum of even digits greater than the sum of odd digits.
Base Case.
check if condition satisfied or not.
If this result is already computed simply return it.
Maximum limit upto which we can place digit . If tight is 0 , means number has already become smaller so we can place any digit , otherwise num [ index ].
if current digit is odd.
if current digit is even.
Function to convert n into its digit vector and uses memo ( ) function to return the required count.
Initialize dp.
Driver Code"
How can the above be solved in Python?
 Write me a code to count numbers in given range such that sum of even digits is greater than sum of odd digits and the other steps required?
assistant
#Here is code to count numbers in given range such that sum of even digits is greater than sum of odd digits and the other steps required.
 def memo ( index , evenSum , oddSum , tight ) :
	if index == len ( v ) :
		if evenSum > oddSum :
			return 1
		else :
			return 0
	if dp [ index ] [ evenSum ] [ oddSum ] [ tight ] != - 1 :
		return dp [ index ] [ evenSum ] [ oddSum ] [ tight ]
	limit = v [ index ] if tight else 9
	ans = 0
	for d in range ( limit + 1 ) :
		currTight = 0
		if d == v [ index ] :
			currTight = tight
		if d % 2 != 0 :
			ans += memo ( index + 1 , evenSum , oddSum + d , currTight )
		else :
			ans += memo ( index + 1 , evenSum + d , oddSum , currTight )
	dp [ index ] [ evenSum ] [ oddSum ] [ tight ] = ans
	return ans
def countNum ( n ) :
	global dp , v
	v . clear ( )
	num = [ ]
	while n :
		v . append ( n % 10 )
		n //= 10
	v . reverse ( )
	dp = [ [ [ [ - 1 , - 1 ] for i in range ( 180 ) ] for j in range ( 180 ) ] for k in range ( 18 ) ]
	return memo ( 0 , 0 , 0 , 1 )
if __name__ == " _ _ main _ _ " :
	dp = [ ]
	v = [ ]
	L = 2
	R = 10
	print ( countNum ( R ) - countNum ( L - 1 ) )