How to check whether a string is odd palindrome in python?

  • Context: Python 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Python String
Click For Summary

Discussion Overview

The discussion focuses on how to check whether a string is an odd palindrome in Python. Participants explore the algorithmic approach to identifying odd palindromes, comparing it to even palindromes, and discussing edge cases related to string lengths.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant shares their understanding of palindrome checking and seeks guidance on writing code for odd palindromes, specifically mentioning the need to identify a middle character.
  • Another participant suggests that checking for odd palindromes can be done by ignoring the middle character, effectively transforming the problem into that of checking an even palindrome.
  • A third participant argues that the same palindrome-checking code can be used for both odd and even-length palindromes without needing to remove the middle character, explaining how the loop operates for different string lengths.
  • A later reply questions the implications of using strings of length 0 or 1, prompting consideration of whether those cases yield the desired results.

Areas of Agreement / Disagreement

Participants express differing views on whether the middle character needs to be removed for odd palindromes, with some asserting it is unnecessary while others suggest it is a valid approach. The discussion remains unresolved regarding the handling of edge cases with string lengths of 0 or 1.

Contextual Notes

There is uncertainty regarding the treatment of strings of length 0 or 1, and how these cases fit into the overall palindrome-checking logic. Additionally, assumptions about the desired outcomes for these edge cases are not fully articulated.

shivajikobardan
Messages
637
Reaction score
54
I am learning to code and 1 thing that surprises me is how do I internalize all the code? I understand the code. I know the algorithm as well. But I want to be able to solve any types of problems(related ones) after learning 1 code. How do I become able to do that? So for that I am first trying with palindrome program.
Here is the palindrome program for even palindrome.

Code:
#palindrome checking

str1="abba"
for i in range(len(str1)//2):
    if(str1[i]==str1[len(str1)-i-1]):
        isPalindrome=True
    else:
        isPalindrome=False
print(isPalindrome)
Now I want to write code for odd palindrome. Don't show me code but show me direction or algorithm so that I can write code on my own.
example of odd palindrome is abbcbba. We are using c as the middle point.
 
Technology news on Phys.org
It's the same method except that you first remove (or ignore) the middle letter to transform it into an even palindrome.

abbXbba is equivalent of checking abbbba, no matter what X is.
 
  • Like
Likes   Reactions: shivajikobardan
jack action said:
It's the same method except that you first remove (or ignore) the middle letter to transform it into an even palindrome.
Exactly the same code as posted works with either odd-length or even-length palindromes. You don't need to remove the middle letter.

For example, if the string is "abba", the loop runs for i = 0 and i = 1, because range(len(str1)//2) includes 0 and 1. If the string is "abcba", the loop also runs for i = 0 and i = 1, because range(len(str1)//2) still includes only 0 and 1.

In case the above isn't clear, for the shorter string, len(str1) == 4, so len(str1)//2 == 2. The call to range(2) is the sequence 0, 1. For the longer string, len(str1) == 5, so len(str1)//2 == 2 again. The call to range(2) is still the sequence 0, 1.

So for the shorter string, the loop runs 2 times; i.e., for i = 0 and for i = 1. When i = 0, the loop compares str1[0] and str1[3] (both are 'a'), and sets isPalindrome to True. When i = 1, the loop compares str1[1] and str1[2] (both are 'b'), and resets isPalindrome to True. The loop then exits, and prints the value of isPalindrome.

For the longer string "abcba", the loop also runs 2 times. When i = 0, the loop compares str1[0] and str1[4] (both are 'a'), and sets isPalindrome to True. When i = 1, the loop compares str1[1] and str1[3] (both are 'b'), and resets isPalindrome to True. The loop then exits, and prints the value of isPalindrome. Note that the loop, as written, doesn't even look at the middle character.

The key to all this is that if n is an even, positive integer, then n // 2 and (n + 1) // 2 both have the same value.
 
Last edited:
  • Like
Likes   Reactions: shivajikobardan and jack action
What happens if you use string lengths of 0 or 1 ? and is that the result your want ?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
Replies
55
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
3K
Replies
5
Views
2K