How to check a string is odd palindrome in python?

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.
 

1. What is a palindrome?

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

2. 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.

3. 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.

4. 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.

5. 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

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
5
Views
994
  • Programming and Computer Science
Replies
1
Views
876
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
885
  • Engineering and Comp Sci Homework Help
Replies
2
Views
947
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top