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 '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