Python How to check 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 and applying them to solve various problems. The user expresses a desire to move beyond understanding algorithms to being able to tackle different coding challenges, starting with palindrome checking. They provide a code snippet for checking even palindromes and seek guidance on how to approach coding for odd palindromes without receiving direct code. Suggestions include focusing on the middle character for odd-length strings and ensuring that the palindrome check stops as soon as a mismatch is found, rather than overwriting the result in each iteration. Additionally, reversing the string for comparison is mentioned as a more intuitive method, although it may not be the most efficient. Overall, the conversation emphasizes understanding the logic behind coding solutions and refining problem-solving skills.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top