Coders Packet

How to check if any permutation of the word is palindrome by using Python

By Harshith Appari

Hello Guys, In this tutorial we gone see how to get the permutations of all the words and to check if any permutation of the word is palindrome and print yes/no statement in Python

We can do this by using itertools module and permutations which is used in itertools

 

from itertools import permutations
a=input("Enter the String : ")
k=[]
p=permutations(a,len(a))
for i in p:
  s=""
  for j in i:
    s=s+j
  e="".join(reversed(s))
  if e==s:
    k.append(s)
if len(k)!=0:
  print("Yes It is a palindrome word")
else:
  print("No It is not a palindrome word")

 

output:

Enter the String : CodeSpeedy
No It is not a palindrome word

 

 

From the itertools, we are importing the permutations.

a is the variable for accepting the string from the user.

k is the empty list that can be used to store the permutations of the word.

p is the permutations of the word a, and it should be arranged in various forms but it should not be greater than the length of the a, and must be equal to length of a and to know the length of a we are using len() function

So here length of a is identified by len(a).

while p is the list and for i in p means taking each word

And take an empty string s

And adding the letters to the empty string

And e is the variable to reverse the word

And checking for e and s are equal means it is a palindrome and appending to the empty list we have created k

And if the length of k is 0, then our answer is No means, not a palindrome

And if the length of k

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Harshith Appari (Harshith)

Download packets of source code on Coders Packet