5

I am currently working on a palindrome-detector (anna, lol, hahah etc) and I am requested to use for-loops.

I want the program to loop through two strings (read them regularly and backwards at the same time while comparing the values). If the values are the same then the palindrome is True; if not, it's False.

My question to you is: how do you get two for-loops to run simultaneously and compare the values of the strings?

Currently doing something like this: (Python 3.0), can post entire code if needed:

   palindrom = True
text2 = ("")
for i in nytext:
    for i in nytext[::-1]:
        text2 = (nytext[::-1] + i)
        if text2 == nytext:
            palindrom = True
        else:
            palindrom = False
return palindrom

Thank you for your help!

EDIT: I might not have been clear enough when describing the problem. The program does this: It lets the user enter a string of text (such as hello my name is lol) and the program is designed to see if this is a palindrome. It is divided into three functions (and a main function).

Function number 1 fixes the text so it is reduced into characters and digits only (so LOL,,,,,, becomes lol for easier reading). Function number 2 is designed to test (using for-loops(!)) if the input is a palindrome. Function number 3 is simply going to post whether it is a palindrome or not.

I have to use for-loops for this and I cannot simply do a comparison such as: backwardtext = text.reverse() if backwardtext == text: print ("It is a palindrome")

I hope this clears things up.

JS.
  • 14,781
  • 13
  • 63
  • 75
user1916173
  • 61
  • 1
  • 6

4 Answers4

8

You use zip

s = 'hannah'

for c_forward,c_backward in zip(s,s[::-1]):
    ...

Maybe a slightly lower level approach would be to loop over the indices (provided your items are indexible):

for i in range(len(s)):
    c_forward = s[i]        #character as you loop going forward
    c_backward = s[-(i+1)]  #character as you loop going backward
    pass #TODO: determine if palindome ... :-p

For future visitors, these don't address all the constraints in the problem, but the easiest way to check if a string is a palindrome in python is to simply do:

def ispal(s):
    return s == s[::-1]
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I know that the questioner asks for `for` loops to be used but an improvement on this would be to use the `all` and `map` functions to remove the need for a `for` loop altogether. – Will Dec 20 '12 at 16:01
  • @Will -- The easiest thing here is just to do `s == s[::-1]`. That's clearly the most pythonic. However, the loop is stated as part of the constraint on the problem. It's important for OP to understand what `zip` does and how it works. Perhaps that is part of what the assignment is getting at. Throwing in a `map` and an `all` isn't going to help with that. :) – mgilson Dec 20 '12 at 16:02
  • This is clearly a programming task aimed at getting knowledge of looping constructs. I was merely trying to point out that in some cases explicit loops can be removed altogether through the use of map/reduce style programming. – Will Dec 20 '12 at 16:04
  • @Will -- Can and should be removed certainly :). Hopefully OP will get to that. – mgilson Dec 20 '12 at 16:06
  • Thank you all for your help, everything is appreciated. Much like Will says, this particular task is focused on loop-knowledge and we are not supposed to use anything else really. – user1916173 Dec 20 '12 at 16:06
3

You can use zip().

def is_palindrome(string):
    return all(x == y for x, y in zip(string, reversed(string)))

The zip() function iterates over two iterables in parallel, stopping at the end of the shortest:

>>> list(zip("abc", range(10)))
[('a', 0), ('b', 1), ('c', 2)]
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
0
def is_palindrome(txt):  
    for i, a in enumerate(txt):  
        if a!=txt[-(i+1)]:  
            return False  
    return True   

txt = 'hannah'
print txt, is_palindrome(txt)
dugres
  • 12,613
  • 8
  • 46
  • 51
0

Just compare it to itself backwards. (Run in Python 3.2)

>>> text = "Palindrome"
>>> text[::-1]
'emordnilaP'
>>> text == text[::-1]
False
>>> text = "PalindromemordnilaP"
>>> text == text[::-1]
True