Check if Binary representation is Palindrome in Python
Given an integer ‘n’, write a Python function that returns true if binary representation of x is palindrome else return false.
Input : n = 9 Output : True Binary representation of n=9 is 1001 which is palindrome as well. Input : n = 10 Output : False Binary representation of n=10 is 1010 which is not palindrome.
We have existing solution for this problem please refer Check if binary representation of a number is palindrome link. We can solve this problem in python very quickly. Approach is very simple,
- Convert given number into it’s binary representation using bin(num) function.
- Now reverse binary representation string of number and compare it with original binary represented string, if both are equal that means binary representation of number is pallindrome else not.
Note : We can use other approach of checking a string is palindrome or not.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course