human
Can you provide a code to queries to count numbers from a range which does not contain digit K in their decimal or octal representation and the other steps required given the following:
Queries to count numbers from a range which does not contain digit K in their decimal or octal representation | Function to check if the given digit ' K ' is present in the decimal and octal representations of num or not.
Stores if the digit exists or not.
Iterate till nums is non - zero.
Find the remainder.
If the remainder is K.
Function to count the numbers in the range [ 1 , N ] such that it doesn ' t ▁ contain ▁ the ▁ digit ▁ ' K ' in its decimal and octal representation.
Stores count of numbers in the range [ 0 , i ] that contains the digit ' K ' in its octal or decimal representation.
Traverse the range [ 0 , 1e6 + 5 ].
Check if i contains the digit ' K ' in its decimal or octal representation.
Update pref [ i ].
Print the answer of queries.
Driver Code.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to queries to count numbers from a range which does not contain digit K in their decimal or octal representation and the other steps required.
 def contains ( num , K , base ) :
	isThere = 0
	while ( num ) :
		remainder = num % base
		if ( remainder == K ) :
			isThere = 1
		num //= base
	return isThere
def count ( n , k , v ) :
	pref = [ 0 ] * 1000005
	for i in range ( 1 , 10 ** 6 + 5 ) :
		present = contains ( i , k , 10 ) or contains ( i , k , 8 )
		pref [ i ] += pref [ i - 1 ] + present
	for i in range ( n ) :
		print ( v [ i ] [ 1 ] - v [ i ] [ 0 ] + 1 - ( pref [ v [ i ] [ 1 ] ] - pref [ v [ i ] [ 0 ] - 1 ] ) , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	K = 7
	Q = [ [ 2 , 5 ] , [ 1 , 15 ] ]
	N = len ( Q )
	count ( N , K , Q )