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
SUMMARY

This discussion focuses on checking whether a string is an odd palindrome using Python. The provided code snippet demonstrates how to check for even palindromes, but the same logic applies to odd palindromes without needing to remove the middle character. The algorithm iterates through half the string length, comparing characters from both ends towards the center. Key insights include that the loop operates identically for both even and odd-length strings, confirming that the middle character does not affect palindrome validation.

PREREQUISITES
  • Understanding of Python programming basics
  • Familiarity with string manipulation in Python
  • Knowledge of control flow structures, specifically loops
  • Concept of palindromes and their properties
NEXT STEPS
  • Implement a function to check for odd palindromes in Python
  • Explore Python string methods for more efficient palindrome checking
  • Learn about time complexity analysis for palindrome algorithms
  • Investigate alternative algorithms for palindrome detection, such as recursive approaches
USEFUL FOR

Beginner Python developers, computer science students, and anyone interested in algorithm design and string manipulation techniques.

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
3K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
3
Views
3K
Replies
5
Views
2K