#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Palindrome checker


    Code:
    def isPalindrome(S): 
    
    for i in range (len(S)//2-1):
    if ord(S[i])==ord(S[-i-1]):
    return True
    else: return False

    this is my code to check palindrome words.
    But, if its a palindrome it would print out True or else it would print False. But the world foolproof isn't a palindrome but the code printed out True. Any help? the word ferrari and racecar worked fine.Sorry i do not know how to insert the code properly into this box. The indentation is a little bit of a problem. But im sure my indentation in the code is correct.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Code:
    def isPalindrome(S):
        for i in range (len(S)//2-1):
            if S[i]==S[-i-1]:
                return True
            else:
                return False
    1) python can test strings for equality. No need for ord(char). You've written extra code, but it's not wrong code.

    2) your range is wrong. Given the palindrome S='aa' the range argument is (2//2)-1 which is 0. The statements in the for loop block won't evaluate, your function returns None by default which is False.

    3) the function must check all character pairs, not merely the first and last characters.


    Here's a palindrome code I wrote for a contest.
    Last edited by b49P23TIvg; December 6th, 2012 at 08:47 AM. Reason: The post looked yucky.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    def isPalindrome(S):
        for i in range (len(S)//2-1):
            if S[i]==S[-i-1]:
                return True
            else:
                return False
    1) python can test strings for equality. No need for ord(char). You've written extra code, but it's not wrong code.

    2) your range is wrong. Given the palindrome S='aa' the range argument is (2//2)-1 which is 0. The statements in the for loop block won't evaluate, your function returns None by default which is False.

    3) the function must check all character pairs, not merely the first and last characters.


    Here's a palindrome code I wrote for a contest.
    i tried //2 and -1 because i have this s[i] i do not want to count the 1st word to the end but only half of it.Sry i have no idea how to express what i'm trying to say,
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    OK, suppose the length of S is odd. Here are some odd numbers, enough to prove generally.

    odds 1 3 5

    Code:
    len(S)
    odd     You'll need to test
    1       nothing
    3       S[0] with S[-1]
    5       S[0] S[1]  with  S[-2] S[-1]
    ...
    The pattern on the left indeed starts to look like range(something) could be used.
    The arguments to range are

    Code:
    odd     You'll need to test                argument of range
    1       nothing                            0
    3       S[0] with S[-1]                    1
    5       S[0] S[1]  with  S[-2] S[-1]       2
    You need a functional dependence on the length of S. You already knew that!
    Code:
    1//2  ==  0
    3//2  ==  1
    5//2  ==  2
    Good! Now let's find a function for even lengths of S.
    Code:
    len(S)  tests                       argument of range
    0       nothing                     0
    2       S[0] with S[-1]             1 
    4       S[:2] with reversed S[-2:]  2
    6       S[:3] with reversed S[-3:]  3
    
    0//2  ==  0
    2//2  ==  1
    4//2  ==  2
    6//2  ==  3
    By gosh by golly the same function works for even and odd len(S).
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo