#๐Ÿ”’ Checking palindrome word: index out of range

63 messages ยท Page 1 of 1 (latest)

muted juniper
#
def check_palindrome(word):
    if len(word) == 0 or len(word) == 1:
        return False
    else:
        begin = 0
        end = -1
        
        while begin != len(word)/2:
            begin += 1
            end -= 1
            if word[begin] != word[end]:
                return False
        
        return True
        
word = "kayak"

if check_palindrome(word):
    print("The word " + word + " matched!")
else:
    print("The word " + word + " didn't matched!")
vagrant urchinBOT
#

@muted juniper

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quasi hedge
#

Where the code says:

while begin != len(word)/2:

What do you expect the value of len(word)/2 to be, for the input word being "kayak"? Now, verify that

#

Can begin ever be equal to that value?

muted juniper
#

ohhhhh

#

I see

#

I mean

#

if begin index reached the middle of the letter

#

it would be success

#

so how would it be turn out?

quasi hedge
#

!e print(len("kayak") / 2)

vagrant urchinBOT
muted juniper
#

so it must be a floor division

quasi hedge
#

right

muted juniper
#

so we can get rid of .5

#

so it be 2 itself

quasi hedge
#

(use // for this.)

muted juniper
#

yea that ofc

#

is there anything else?

quasi hedge
#

I think "" and "x" (for example) should be counted as palindromes, yes?

muted juniper
#

yea it's the same

#

even 2 letter

quasi hedge
#

there are maybe some more elegant ways to write it, but I think the issue is resolved and you learned the main point of the exercise

muted juniper
#

like aa -> correct. ch - > incorrect

muted juniper
#

I appreciated your help!

#

I remember I did this with recursion and successed

#

is there any thoughts about this code?

quasi hedge
#

nothing worth talking about at this stage, I think

muted juniper
#

bro

#

this code

#
def check_palindrome(word):
    if len(word) == 0 or len(word) == 1:
        return True 
    else:
        begin = 0
        end = -1
        
        while begin != len(word)//2:
            begin += 1
            end -= 1
            if word[begin] != word[end]:
                return False
        
        return True
        
word = "abc"

if check_palindrome(word):
    print("The word " + word + " matched!")
else:
    print("The word " + word + " didn't matched!")

shows the abc word matched

#

I edited this one.

quasi hedge
#

consider when the letters are checked, vs. when begin and end are updated.

muted juniper
#

it skipped the if letters are not equal thats why

quasi hedge
#

it didn't check the first and last letters against each other.

#

because begin and end change first, and then the comparison is tried.

muted juniper
#

I changed it now:

            if word[begin] != word[end]:
                return False
            else:
                begin += 1
                end -= 1
        
#

so it first must check if the letters are not equal

quasi hedge
#

yes.

muted juniper
#

and if that's false

#

then it adds

#

and decrement

quasi hedge
#

(We don't actually need else, because return means the rest of the code won't be reached anyway.)

muted juniper
#
            if word[begin] != word[end]:
                return False
           
            begin += 1
            end -= 1
        
#

like this?

quasi hedge
#

yeah

#

a general programming technique/style :)

muted juniper
#

Thanks for your help!

quasi hedge
#

oh, also I assume you know

#

you can just reverse the string and compare strings? :)

muted juniper
#

btw I'm certified in python

#

I came back to it

#

cuz I forgot and studied C++

#

just getting a review.

quasi hedge
#

welcome back. I never really cared about any certifications, it's a big world out there and people have all kinds of opinions and standards

muted juniper
#

Alr wanna close this post?

#

Cya!

#

!close

vagrant urchinBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.