How to check a string is odd palindrome in python?

  • Python
  • Thread starter shivajikobardan
  • Start date
  • Tags
    Python String
In summary, the conversation is about the topic of coding and how to internalize and solve different types of problems after learning one code. The specific problem being discussed is how to write code for odd palindromes, with an example given. The algorithm for checking palindromes is also discussed, along with a suggestion for a more functional approach and a potential issue with the given code.
  • #1
shivajikobardan
674
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
  • #2
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)
 
  • #3
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.
 

FAQ: How to check a string is odd palindrome in python?

What is a palindrome?

A palindrome is a word, phrase, or sequence that reads the same backward as forward.

How do you check if a string is a palindrome in python?

To check if a string is a palindrome in python, you can use the reverse() or [::-1] function to reverse the string and then compare it to the original string. If they are the same, the string is a palindrome.

What is an odd palindrome?

An odd palindrome is a palindrome with an odd number of characters. For example, "level" and "radar" are odd palindromes.

What is the code to check if a string is an odd palindrome in python?

The code to check if a string is an odd palindrome in python would involve converting the string to lowercase, removing all non-alphanumeric characters, and then using the reverse() or [::-1] function to reverse the string and compare it to the original string. If they are the same, the string is an odd palindrome.

Can you provide an example of checking for an odd palindrome in python?

Yes, here is an example code that checks if a string is an odd palindrome in python:

def is_odd_palindrome(string):
 string = string.lower()
 string = re.sub("[^a-z0-9]", "", string)
 if string == string[::-1]:
  return True
 else:
  return False

string = "level"
print(is_odd_palindrome(string)) # Output: True

Similar threads

Replies
3
Views
2K
Replies
5
Views
1K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
4
Views
1K
Back
Top