-2

What would be the loop content in the for loop?

a=raw_input("Enter a string :")
print a
length = len(a)
p = a[::-1]
print p
if a == p:
   print "palindrome"
else:
   print "Not palindrome"
for i in range(len(a)):
Tanay Suthar
  • 453
  • 3
  • 8
  • 19
  • Relevant http://stackoverflow.com/questions/248161/palindrome-detection-efficiency – Celeo Nov 20 '14 at 23:47
  • your question makes little sense, are you checking for palindromes or trying to manipulate strings to become palindromes? – Padraic Cunningham Nov 20 '14 at 23:50
  • I am trying to manipulate strings to become palindromes. For example string "aaab" is not a palindrome. So, I need to remove b in order to be "aaa" palindrome. So what would be the logic behind it? – Tanay Suthar Nov 21 '14 at 02:10

2 Answers2

1

Check whether the length of the string is odd or even. If odd try to preserve the middle character.

if len(s)%2:
    return s[:len(s)//2] + s[len(s)//2] + s[:len(s)//2][::-1]
else:
    return s[:len(s)//2] + s[:len(s)//2][::-1]
-2

This checks if string s is a palindrome

s='hiih'
s[0:int(len(s)/2)] == s[int(len(s)/2):len(s)][::-1]
Erik
  • 307
  • 1
  • 9
  • It might not answer the vague question. What I state is true however. Why vote down? – Erik Nov 21 '14 at 00:48
  • maybe because it does not answer the question, *and* is nowhere near an elegant solution, even buggy? `s==s[::-1]` does the trick, and is far more elegant. – ch3ka Nov 21 '14 at 02:08
  • hm, I'm afraid you're right, I guess I earned this one :-) – Erik Nov 21 '14 at 10:02