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

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Python String
AI Thread Summary
The discussion centers on the challenge of internalizing coding concepts, particularly in relation to palindrome checking algorithms. A user is learning to code and seeks to understand how to apply knowledge of even palindromes to odd palindromes. They provide an example of an even palindrome program and inquire about the algorithm for odd palindromes without requesting specific code. The response clarifies that the same code structure can be used for both even and odd-length palindromes, emphasizing that the middle character can be ignored during the comparison process. The explanation highlights that the loop iterates based on half the string length, allowing it to compare characters symmetrically from both ends towards the center. It also notes that for strings of length 0 or 1, the logic still holds, reinforcing the importance of understanding how the algorithm operates across different string lengths. Overall, the discussion underscores the significance of grasping foundational coding principles to tackle various problem types effectively.
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 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 shivajikobardan and jack action
What happens if you use string lengths of 0 or 1 ? and is that the result your want ?
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top