human
Count of integers up to N which represent a Binary number | Function to return the count.
PreCompute and store the powers of 2.
If the current last digit is 1.
Add 2 ^ ( ctr - 1 ) possible integers to the answer.
If the current digit exceeds 1.
Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to count of integers up to N which represent a Binary number and the other steps required.
 def countBinaries ( N ) :
	powersOfTwo = [ 0 ] * 11
	powersOfTwo [ 0 ] = 1
	for i in range ( 1 , 11 ) :
		powersOfTwo [ i ] = powersOfTwo [ i - 1 ] * 2
	ctr = 1
	ans = 0
	while ( N > 0 ) :
		if ( N % 10 == 1 ) :
			ans += powersOfTwo [ ctr - 1 ]
		elif ( N % 10 > 1 ) :
			ans = powersOfTwo [ ctr ] - 1
		ctr += 1
		N = N // 10
	return ans
N = 20
print ( countBinaries ( N ) )