Palindrome in python

Palindrome in python

Given a string, the program checks if the string is palindrome or not. A string is called palindrome if the reverse of the string is equal to the original string. 

For example, the string 'madam' is a palindrome as the reverse of the string also forms the same word 'madam'.
Meanwhile, the string 'Apple' is not a palindrome, the reverse of the string forms the word 'elppA'.

Method 1 :

In this method, the entire string is reversed. If the reversed string is equal to the original string then the string is said to be palindrome.

Procedure :

1. Start.
2. Get the string from the user.
3. Reverse the string.
4. If the reversed string is equal to the original string, the string is palindrome.
5. Print the result.
6. End.

Code :

#get the string as input from the user
s = str(input("Enter the string : "))
#reverse the string
s1 = s[::-1]
#if the given string is equal to the reversed string the string is palindrome
if(s == s1):
print("palindrome.")
else :
print("Not a palindome.")

Method 2 :

In this method, an iterative approach is used to iterate through each element of the string. The iteration is undergone from the first element to the middle element, and checks if the ith element is same as the last ith element. If the all the ith element is same as the last ith element then the string is palindrome.

Procedure :

1. Start.
2. Get the string as input from the user.
3. initialize a flag variable.
4. loop from 0 to (length of string)/2
    => if ith element is equal to element at index len(s)-i-1 => continue
    => else => break the loop => flip flag
5. Print the result accordingly.
6. End

Code :

#get the string as input from the user
s = str(input("Enter the string : "))
#initialize flag as 0
flag=0
#loop from first element to middle element
#check if the ith element is equal to the last ith element
#if not equal flip flag as 1
for i in range(int(len(s)/2)):
if(s[i]==s[len(s)-i-1]):
continue
else :
flag = 1
break
#if flag is 1 => not palindrome
#if flag is 0 => palindrome
if(flag == 0):
print("Palindrome")
else :
print("Not a palindrome")

Method 3 :

In this method, the second half of the string is reversed and checks if it is same as the first half of the string. If both are equal then the given string is a palindrome. This method will be more efficient than the first method as we are reversing only half of the string whereas in the first method we will be reversing the entire string.

Procedure :

1. Start.
2. Get the string as input from user.
3. Split the string into two halves.
4. Reverse the second half of the string.
5. if the reversed string is equal to the first half of the string, the string is palindrome.
6. Else the string is not palindrome.
7. print the result.
8. End.

Code :

#get the string as input from the user
s = str(input("Enter the string : "))
#calculate the length of the string
n = len(s)
# if the length of the string is odd
#split the string into two halves with the middle element as the center of division
#reverse the second half of the string
#if the first half of the string is same as the reversed second half string#the string is palindrome
#else the string is not a palindrome
if(n % 2 == 1):
s1 = s[:int(n/2)]
s2 = s[int(n/2)+1::]
s2 = s2[::-1]
# if the length of the string is even
#split the string into two halves
#reverse the second half of the string
#if the first half of the string is same as the reversed second half string#the string is palindrome
#else the string is not a palindrome
else :
s1 = s[:int(n/2)]
s2 = s[int(n/2)::]
s2 = s2[::-1]
print(s1)
print(s2)
if(s1 == s2):
print("palindrome.")
else :
print("Not a palindome.")

Sample input 0 :

s = 'madam'

Sample output 0 :

Palindrome

Sample input 1 :

s = 'river'

Sample output 1 :

Not a Palindrome

Sample input 2 :

s = 'noon'

Sample output 2 :

Palindrome

Output screenshot :

palindrome in python




palindrome in python




palindrome in python

Comments

Popular posts from this blog

Finding the percentage in python - HackerRank solution

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

ginortS in Python - HackerRank solution