human
Find the occurrence of the given binary pattern in the binary representation of the array elements | Function to return the binary representation of n.
Array to store binary representation.
Counter for binary array.
Storing remainder in binary array.
To store the binary representation as a string.
Function to return the frequency of pat in the given txt.
A loop to slide pat [ ] one by one.
For current index i , check for pattern match.
If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ].
Function to find the occurrence of the given pattern in the binary representation of elements of arr [ ].
For every element of the array.
Find its binary representation.
Print the occurrence of the given pattern in its binary representation.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to find the occurrence of the given binary pattern in the binary representation of the array elements and the other steps required.
 def decToBinary ( n ) :
	binaryNum = [ 0 for i in range ( 32 ) ]
	i = 0
	while ( n > 0 ) :
		binaryNum [ i ] = n % 2
		n = n // 2
		i += 1
	binary = " "
	for j in range ( i - 1 , - 1 , - 1 ) :
		binary += str ( binaryNum [ j ] )
	return binary
def countFreq ( pat , txt ) :
	M = len ( pat )
	N = len ( txt )
	res = 0
	for i in range ( N - M + 1 ) :
		j = 0
		while ( j < M ) :
			if ( txt [ i + j ] != pat [ j ] ) :
				break
			j += 1
		if ( j == M ) :
			res += 1
			j = 0
	return res
def findOccurrence ( arr , n , pattern ) :
	for i in range ( n ) :
		binary = decToBinary ( arr [ i ] )
		print ( countFreq ( pattern , binary ) , end = " ▁ " )
arr = [ 5 , 106 , 7 , 8 ]
pattern = "10"
n = len ( arr )
findOccurrence ( arr , n , pattern )