xxxxxxxxxx
# 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)
xxxxxxxxxx
s = input('enter string: ')
def palindrome(string):
x = ""
for i in string:
x = i + x
return x
if s == palindrome(s):
print('its a palindrome')
else:
print('its not a palindrome')
xxxxxxxxxx
p = list(input())
for i in range(len(p)):
if p[i] == p[len(p)-1-i]:
continue
else:
print("NOT PALINDROME")
break
else:
print("PALINDROME")
xxxxxxxxxx
myString = "aba"
if myString == myString[::-1]:
print("The string '" + myString + "' is a palindrome")
else:
print("The string '" + myString + "' is not a palindrome")
xxxxxxxxxx
def is_palindrome(s):
reverse = s[::-1]
if (s == reverse):
return True
return False
s1 = 'racecar'
s2 = 'hippopotamus'
print('\'racecar\' a palindrome -> {}'.format(is_palindrome(s1)))
print('\'hippopotamus\' a palindrome -> {}'.format(is_palindrome(s2)))
# output
# 'racecar' is a palindrome -> True
# 'hippopotamus' is a palindrome -> False
xxxxxxxxxx
word = input() if str(word) == str(word)[::-1] : print("Palindrome") else: print("Not Palindrome")
xxxxxxxxxx
string = input("Type a string: ")
if string[::-1] == string:
print(string,"This string is Palindrome")
else:
print(string,"This string is not Palindrome")
xxxxxxxxxx
s = "001100"
if s == s[::-1]:
print(s, "is a Palindrome string.")
else:
print("Not a palindrome string.")
xxxxxxxxxx
"""
This implementation checks whether a given
string is a palindrome. A string is
considered to be a palindrome if it reads the
same forward and backward. For example, "kayak"
is a palindrome, while, "door" is not.
Let n be the length of the string
Time complexity: O(n),
Space complexity: O(1)
"""
def isPalindrome(string):
# Maintain left and right pointers
leftIdx = 0
rightIdx = len(string)-1
while leftIdx < rightIdx:
# If chars on either end don't match
# string cannot be a palindrome
if string[leftIdx] != string[rightIdx]:
return False
# Otherwise, proceed to next chars
leftIdx += 1
rightIdx -= 1
return True
print(isPalindrome("kayak")) # True
print(isPalindrome("door")) # False
xxxxxxxxxx
is_palindrome = lambda phrase: phrase == phrase[::-1]
print(is_palindrome('anna')) # True
print(is_palindrome('rats live on no evil star')) # True
print(is_palindrome('kdljfasjf')) # False