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 ?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
hi; i purchased 3 of these, AZDelivery 3 x AZ-MEGA2560-Board Bundle with Prototype Shield and each is reporting the error message below. I have triple checked every aspect of the set up and all seems in order, cable devices port, board reburn bootloader et al . I have substituted an arduino uno and it works fine; could you help please Thanks Martyn 'avrdude: ser_open(): can't set com-state for "\\.\COM3"avrdude: ser_drain(): read error: The handle is invalid.avrdude: ser_send(): write...

Similar threads

Back
Top