How to check a string is odd palindrome in python?

Thread Starter

terabaaphoonmein

Joined Jul 19, 2020
111
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.
 

Thread Starter

terabaaphoonmein

Joined Jul 19, 2020
111
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)
 

strantor

Joined Oct 3, 2010
6,782
Why check if the middle letter is "c"? It makes no difference what the middle letter is. If the string has odd number of characters, simply ignore the middle character.
 

MrChips

Joined Oct 2, 2009
30,711
Don’t focus on programming.
Don’t look at other people’s code.

Before you start writing a program you must have formulated the solution already. You do this by writing out the solution in plain language. The solution must be void of computer jargon. The solution is independent of the programming language. In other words, the solution is the same regardless of what programming language you plan on using.
 

Papabravo

Joined Feb 24, 2006
21,159
It is naïve to expect that you can acquire this skill quickly and easily. It requires constant and concerted effort over a long period of time. In my case it was in the neighborhood of nine years from the time I wrote my first FORTRAN program (1962) until writing an instruction set interpreter (1971) before I developed the confidence required to break a problem down into component parts and solve them one by one. In this process you will suffer multiple failures which is not necessarily a bad thing. We learn more from our failures than our successes.
 

MrChips

Joined Oct 2, 2009
30,711
It is naïve to expect that you can acquire this skill quickly and easily. It requires constant and concerted effort over a long period of time. In my case it was in the neighborhood of nine years from the time I wrote my first FORTRAN program (1962) until writing an instruction set interpreter (1971) before I developed the confidence required to break a problem down into component parts and solve them one by one. In this process you will suffer multiple failures which is not necessarily a bad thing. We learn more from our failures than our successes.
I would say we don't learn much from successes.

When we assemble something, software and hardware, and it works perfectly first time, we didn't learn anything beyond what we already knew.

When things don't work is a great opportunity to learn and hone our problem solving skills.
 

Papabravo

Joined Feb 24, 2006
21,159
I would say we don't learn much from successes.

When we assemble something, software and hardware, and it works perfectly first time, we didn't learn anything beyond what we already knew.

When things don't work is a great opportunity to learn and hone our problem solving skills.
It is hard to keep going when all your efforts end in failure. You need to succeed some of the time. Collaboration is a great way to improve the odds, if you can manage it.
 

MrChips

Joined Oct 2, 2009
30,711
It is hard to keep going when all your efforts end in failure. You need to succeed some of the time. Collaboration is a great way to improve the odds, if you can manage it.
Certainly. It is when we discover and correct our mistakes that we learn something. I do it everyday!
 

MrSalts

Joined Apr 2, 2020
2,767
First, start with a block diagram that describes your problem-solving technique as you personally would solve whether a word is a palindrome.

how long is the string?
Is the string an even or odd number of characters?
- if odd, the middle character needs no match
- if even, all characters need matches
Then work from the outside in or the inside out checking if first and last match, then next and next snd so on until you reach int(stringLength /2) cycles.
 

ApacheKid

Joined Jan 12, 2015
1,533
Don’t focus on programming.
Don’t look at other people’s code.

Before you start writing a program you must have formulated the solution already. You do this by writing out the solution in plain language. The solution must be void of computer jargon. The solution is independent of the programming language. In other words, the solution is the same regardless of what programming language you plan on using.
This is good advice Tera. Use pen and paper and list several of the simples possible example, what will go in and what you expect out, then add a few more slightly more complex examples. Study these because this is your specification and the examples can serve as unit tests once you're done.

If you jump into writing code too soon, you'll reduce the chances of devising a good solution.
 

Ya’akov

Joined Jan 27, 2019
9,070
A high level heuristic I use is something like: problem definition (what am I given and what must I return), algorithm(s) (language-independent stepwise solution), adaptation of algorithms to the strengths and weaknesses of the target language, optimization *after* testing and/or profiling.
 
Top