Check if Binary representation is Palindrome in Python
Last Updated :
14 Mar, 2023
Given an integer ānā, write a Python function that returns true if binary representation of x is palindrome else return false. Examples:
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 palindrome else not.
-
Note : We can use other approach of checking a string is palindrome or not.
Python
# Function to check if binary representation of
# a number is palindrome or not
def binarypalindrome(num):
# convert number into binary
binary = bin(num)
# skip first two characters of string
# because bin function appends '0b' as
# prefix in binary representation of
# a number
binary = binary[2:]
# now reverse binary string and compare
# it with original
return binary == binary[-1::-1]
# Driver program
if __name__ == "__main__":
num = 9
print binarypalindrome(num)
Using iteration:
The following is an iterative approach to the problem of checking if the binary representation of a number is a palindrome:
The first step in the function is to convert num into its binary representation using the bin function. The bin function returns a string that starts with '0b', so we use string slicing to remove these characters and just keep the binary representation.
Next, the function initializes two variables, left and right, to keep track of the indices of the left and right characters in the binary string. left is initialized to 0, while right is initialized to the length of the binary string minus 1.
The function then enters a while loop that continues as long as left is less than right. Inside the loop, the function compares the character at the left index to the character at the right index. If these characters are not equal, the function returns False immediately to indicate that the binary string is not a palindrome. If the characters are equal, the function increments left by 1 and decrements right by 1 and continues the loop.
If the loop completes, it means that all the characters in the binary string were compared and found to be equal. In this case, the function returns True to indicate that the binary string is a palindrome.
Finally, the code includes a driver program that tests the binarypalindrome function by calling it with the value 9 and printing the result.
Python3
def binarypalindrome(num):
# convert number into binary
binary = bin(num)[2:]
# initialize variables for loop
left = 0
right = len(binary) - 1
# loop through the binary string, comparing the left and right characters
while left < right:
if binary[left] != binary[right]:
return False
left += 1
right -= 1
# if the loop completes, the binary string must be a palindrome
return True
# Driver program
if __name__ == "__main__":
num = 9
print(binarypalindrome(num))
Time complexity: O(n), where n is the length of the binary string. This is because the while loop runs in linear time, with the number of iterations being equal to the length of the binary string.
Auxiliary space: O(1), as the solution only uses a constant amount of additional memory to store the variables left, right, and binary. No additional data structures are used, so the space complexity does not depend on the size of the input.
Similar Reads
Python Program To Check If A Singly Linked List Is Palindrome Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. METHOD 1 (Use a Stack): A simple solution is to use a stack of list nodes. This mainly invol
6 min read
Test if List is Palindrome - Python We are given a list we need to find that given list is a palindrome. For example a = [1, 2, 3, 2, 1] we need to check whether it is a palindrome or not so that the output should be 'True'.Using SlicingWe can check if a list is a palindrome by comparing it with its reverse using slicing ([::-1]). If
2 min read
Python Program to Check if a String is Palindrome or Not The task of checking if a string is a palindrome in Python involves determining whether a string reads the same forward as it does backward. For example, the string "madam" is a palindrome because it is identical when reversed, whereas "hello" is not. Using two pointer techniqueThis approach involve
3 min read
Python Program To Check If A Linked List Of Strings Forms A Palindrome Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco
2 min read
Python Program To Check String Is Palindrome Using Stack A palindrome is a sequence of characters that reads the same backward as forward. Checking whether a given string is a palindrome is a common programming task. In this article, we will explore how to implement a Python program to check if a string is a palindrome using a stack. Example Input: str =
3 min read
Python program to check if given string is vowel Palindrome Given a string (may contain both vowel and consonant letters), remove all consonants, then check if the resulting string is palindrome or not. Examples: Input : abcuhuvmnba Output : YES Explanation : The consonants in the string "abcuhuvmnba" are removed. Now the string becomes "auua". Input : xayzu
5 min read
Python program to print an array of bytes representing an integer Given an integer N, the task is to write a Python program to represent the bytes of this number as an array. A byte is a group of 8 bits. Any integer can be represented in the form of bytes and bits. We generally use hexadecimal codes to represent a byte. A single hexadecimal character can represent
3 min read
Python program to check if number is palindrome (one-liner) In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number
3 min read
Integer to Binary String in Python We have an Integer and we need to convert the integer to binary string and print as a result. In this article, we will see how we can convert the integer into binary string using some generally used methods.Example: Input : 77Output : 0b1001101Explanation: Here, we have integer 77 which we converted
4 min read
Python Program For Checking Linked List With A Loop Is Palindrome Or Not Given a linked list with a loop, the task is to find whether it is palindrome or not. You are not allowed to remove the loop. Examples: Input: 1 -> 2 -> 3 -> 2 /| |/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input: 1 -> 2 -> 3 -> 4 /| |/ ------- 1
4 min read