Return the longest odd-length palindrome centered at the int index in the str. Assume that the index is valid, that the length of the str is at least one, and that there are only lowercase letters: no punctuation, spaces, or uppercase letters.

I just want to know what this question is asking. I don't quite understand...
supposed to use this function --> get_odd_palindrome_at(str, int)

Well, now that I know what I'm supposed to do...
i don't know how to do it.

let's see if I can explain what I think the question is asking...:)

e.g.
you have a string s = "racecar", say
then the function get_odd_palindrome_at( str, int ) is supposed to find the longest odd palindrome, so it starts at index 3, say.
s[ 3 ] = "e". Now you have to start checking from that index and you have to check the next character in the left with the next character in the right and if they are equal save their indexes so that when you reach a point where they differ, you could just slice the string off, starting from the index for the left side and ending at the index for the right side...

hope this is not too confusing...:)
here's a hint:

s = "ricecar" #don't know why i thought of that :)
so the function has to return "cec" :)

if I am getting something wrong pls post a reply. And if you need more assistance I'll explain the solution :)

Edited 7 Years Ago by masterofpuppets: n/a

Actually I was wondering if you could post up your solution, I currently have this:

i = 1
new_word = word[inty]
while i < 3 and (word[inty - i] == word[inty + i]):
new_word += word[inty + i]
new_word = word[inty - i] + new_word
i += 1
return new_word

And obviously the first condition on the while is just for testing, i can't get it to not give me the 'index out of range' bug, as in.. I can't figure out how to check if something actually exists in the index or not :S

well here's my solution to the problem. I think I tested it enough but there might be some more bugs to clean:

def get_odd_palindrome_at( s, index ):
    # start and end point are at the index
    startIndex, endIndex = index, index
    # check if the startIndex doesn't go below 0 and the endIndex doesn't exceed the length of the word
    while startIndex >= 0 and endIndex < len( s ): 
        #compare the strings at the start and end locations. if they are equal go to next positions, i.e decrease start and increase end
        if s[ startIndex ] == s[ endIndex ]: 
            startIndex -= 1
            endIndex += 1
        else:
            break #don't need to go further...
        
    #this small loop constructs the final string using the start and the end indexes
    finalString = "" 
    countIndex = startIndex + 1 #needed because in the previous loop start is decreased by one
    while countIndex < endIndex:
        finalString += s[ countIndex ] #add the char to the new string
        countIndex += 1
    return finalString

hope this makes it a bit clearer :)

Edited 7 Years Ago by masterofpuppets: n/a

Mine's a bit shorter lol.. I kind of like it more for that reason.

Are you sure it isn't just easier to check whether or not the index exists, as opposed to going through all of your steps?
Btw we're not allowed to use break or continue :P

here are some test results:

>>> print get_odd_palindrome_at( "racecar", 3 )
racecar
>>> print get_odd_palindrome_at( "ricecar", 3 )
cec
>>> print get_odd_palindrome_at( "ricecac", 5 )
cac
>>> print get_odd_palindrome_at( "ricecar", 1 )
i
>>> print get_odd_palindrome_at( "ricecar", 0 )
r
>>> print get_odd_palindrome_at( "rirecar", 2 )
r
>>> print get_odd_palindrome_at( "rirecar", 1 )
rir
>>>

Did you read what I just wrote about whether or not there's a condition for checking the existance of an index?
Also, how would the code change if you were detecting even palindromes, with the leftmost inner letter being at the index

eg: ('abcdeed', 4)
Would give you 'deed', but
('abcdeed', 5)
would give you '' (empty string)
and ('abcbeed' , 4) would give you 'ee'.

yeah I guess it could be simplified :) i'll try to do it when i have some more free time though. That's just what i came up with in like 10 mins so... :) I'm sure it could be made shorter :)

I can't use break.
Well this is what I came up with, but I still get a fail.

def get_odd_palindrome_at(phrase, index):
    c = 0
    phrase_letters = phrase[index]
    while (index + c) < len(phrase) -1 and (index - c) >= 0:
        if phrase[index+1] == phrase[index-1]:
            c+=1
            phrase_letters == phrase[index+c]+phrase_letters+phrase[index-c]
    if index == 0:
        print phrase[index]
    else:
        return phrase_letters

When I tried putting in phrase = 'aabbccbbaa' and index = 1, i'd get an infinite loop... er it worked before. Can someone just check over my code and tell me what's wrong with it @_@?

hi
your infinite loop comes from the fact that you never update c or index anywhere besides in the if statement, so if the if statement is not true you get an infinite loop :) here's my versioin again with the break removed :)

def get_odd_palindrome_at( s, index ):
    # start and end point are at the index
    goFurther = True
    startIndex, endIndex = index, index
    # check if the startIndex doesn't go below 0 and the endIndex doesn't exceed the length of the word
    while startIndex >= 0 and endIndex < len( s ) and goFurther: 
        #compare the strings at the start and end locations. if they are equal go to next positions, i.e decrease start and increase end
        if s[ startIndex ] == s[ endIndex ]: 
            startIndex -= 1
            endIndex += 1
        else:
            goFurther = False #don't need to go further..
 
    #this small loop constructs the final string using the start and the end indexes
    finalString = "" 
    countIndex = startIndex + 1 #needed because in the previous loop start is decreased by one
    while countIndex < endIndex:
        finalString += s[ countIndex ] #add the char to the new string
        countIndex += 1
    return finalString

Edited 7 Years Ago by masterofpuppets: n/a

Did you read what I just wrote about whether or not there's a condition for checking the existance of an index?
Also, how would the code change if you were detecting even palindromes, with the leftmost inner letter being at the index

eg: ('abcdeed', 4)
Would give you 'deed', but
('abcdeed', 5)
would give you '' (empty string)
and ('abcbeed' , 4) would give you 'ee'.

hi I have changed my code according to your specification, i.e. with the leftmost inner letter being at the index and I think it works

def get_even_palindrome_at( s, index ):
    if s[ index ] == s[ index + 1 ]: #check the letter to the right of the index
        startIndex = index - 1
        endIndex = index + 2 
        while startIndex >= 0 and endIndex < len( s ): 
            if s[ startIndex ] == s[ endIndex ]: 
                startIndex -= 1
                endIndex += 1
            else:
                break
        finalString = "" 
        countIndex = startIndex + 1
        while countIndex < endIndex:
            finalString += s[ countIndex ]
            countIndex += 1
        return finalString
    else:
        return ""

note that I haven't included any index checking so if index + 1 > len( s ) - 1 the program would complain. This can be simply resolved by adding an if statement in the beginning. I am using my version so the break is still there but i posted a method to remove it :)

forgot to add some test results:

>>> get_even_palindrome_at( "ouuo", 2 )
''
>>> get_even_palindrome_at( "ouuo", 1 )
'ouuo'
>>> get_even_palindrome_at( "abcdeed", 4 )
'deed'
>>> get_even_palindrome_at( "abcdeed", 5 )
''
>>> get_even_palindrome_at( "abcbeed", 4 )
'ee'
>>>
This question has already been answered. Start a new discussion instead.