Find the n | utility function which is used to convert binary string into integer ; convert binary string into integer ; function to find nth binary palindrome number ; stores the binary palindrome string ; base case ; add 2 nd binary palindrome string ; runs till the nth binary palindrome number ; remove curr binary palindrome string from queue ; if n == 0 then we find the n 'th binary  palindrome so we return our answer ; calculate length of curr binary palindrome string ; if length is even . so it is our first case we have two choices ; if length is odd . so it is our second case we have only one choice ; Driver code ; Function Call
def convertStringToInt ( s ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT ans = ans * 2 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def getNthNumber ( n ) : NEW_LINE INDENT q = [ ] NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT n = n - 1 NEW_LINE q . append ( "11" ) NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT curr = q . pop ( 0 ) NEW_LINE n -= 1 NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return convertStringToInt ( curr ) NEW_LINE DEDENT lenn = len ( curr ) NEW_LINE if ( len ( curr ) % 2 == 0 ) : NEW_LINE INDENT q . append ( curr [ 0 : int ( lenn / 2 ) ] + "0" + curr [ int ( lenn / 2 ) : ] ) NEW_LINE q . append ( curr [ 0 : int ( lenn / 2 ) ] + "1" + curr [ int ( lenn / 2 ) : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT midChar = curr [ int ( lenn / 2 ) ] NEW_LINE q . append ( curr [ 0 : int ( lenn / 2 ) ] + midChar + curr [ int ( lenn / 2 ) : ] ) NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT n = 9 NEW_LINE print ( getNthNumber ( n ) ) NEW_LINE