How to check 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 if a string is an odd palindrome in Python. The user provides an example of an odd palindrome, "abcba," and seeks guidance on the algorithm rather than the code itself. Key insights include the importance of identifying the middle character and comparing characters from both ends towards the center. Additionally, it is recommended to stop the loop upon finding a mismatch to enhance efficiency.

PREREQUISITES
  • Understanding of Python programming basics
  • Familiarity with string manipulation in Python
  • Knowledge of control flow structures (loops and conditionals)
  • Concept of palindromes and their properties
NEXT STEPS
  • Implement a function to check for odd palindromes in Python
  • Explore string reversal techniques in Python
  • Learn about early exit strategies in loops for efficiency
  • Study advanced string manipulation methods in Python
USEFUL FOR

Beginner Python developers, coding 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
Code:
#palindrome checking

str1="abcba"
if(str1[len(str1)//2]=="c"):
    for i in range(len(str1)//2):
        if(str1[i]==str1[len(str1)-i-1]):
            isPalindrome=True
        else:
            isPalindrome=False
print(isPalindrome)
 
If you want more functional code and care less about the fastest implementation then I would reverse the string first and compare letter by letter. This just makes it more intuitive. https://stackoverflow.com/questions/931092/reverse-a-string-in-python

In your loop you check the letter with the mirror position on the opposite side, which looks good, but you overwrite the isPalinidrome indicator and this could lead to issues. All that matters in your code is the last iteration of the loop. I would modify this so that any time this returns False then we stop the loop and return False.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
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