Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description A palindrome is a string that reads the same forward or backwards. P

ID: 3732108 • Letter: D

Question

Description A palindrome is a string that reads the same forward or backwards. Putting it another way, ls its reversal. So ogopogo an Your task is simple. Find the length of the longest odd-length palindrome that is a substring of a given string. We do not care about palindromes of even length today. Input Input consists of a single line with a single string. This string will contain only lowercase letters and will have length between 1 and 1000. Output Output a single integer k on a single line. This should be the length of the longest odd-length palindrome that appears as a substring of the input string. Sample Input 1 banana Sample Output 1 Explanation for Sample 1 The string anana is a palindrome with length 5, and there are no longer odd-length palin- dromes.

Explanation / Answer

Here is a simplest solution:

my_string = input()
count = []
for x in range(len(my_string)):
    for y in range(x, len(my_string)):
        if my_string[x : y+1] == my_string[y : x-1 : -1]:
            if(y + 1 - x) % 2 != 0:
                count.append(y + 1 - x)
print(max(count))

Remember, above program doesn't take the whole string as a substring even if it is a Odd-Palindrome.

For Example:

If you input string like: 'madam'

Output will be 3 not 5 because above program doesn't consider the whole string a substring of itself like we do mathematics.

Let me know if you have any queries or you want any change or help.

Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote