Fundamental Functions: 8. Palindrome (different solution?)

Screen Link:

My Code:

def is_palindrome(dna):  
    if dna[0:-1:1] == dna[-1:0:-1]:
        return True
    else:
        return False

Although my answer was correct, the solution was a bit different. So, I was wondering whether there is something I should watch out with my ‘logic’ or code in a different scenario. And, for this reason, I should use the proposed solution.

Thank you

1 Like

#My code was for the solution:
def is_palindrome(dna):

if dna == dna[::-1]:
    return True
return False

Solution Code:
def is_palindrome(dna):

n = len(dna)
for i in range(n // 2):
    if dna[i] != dna[n - i - 1]:
        return False
return True

Solution code is saving execution time more efficiently, because it’s looping over 1/2 of the length of any string and if 1st half is the same with the reverse order of last half, then the checking is actually completed and no need to check full length. So, it’s saving us from the time redundancy.

1 Like

That’s not entirely correct. @boemer00’s solution is much faster compared to the official solution as the length of the string increases.

@boemer00

Good question.

There are almost always cases where you will find multiple ways to solve or approach a problem. As you solve more problems AND read more solutions, your own problem-solving skills will also improve.

I would say it’s usually better to think through a more obvious solution using a simpler approach and then trying to improve upon it. For example, you tried to use python’s indexing features to your advantage. But it would still be helpful to you, in the long run, to think of different ways to solve the same problem.

This becomes especially helpful when you eventually reach the stage of going through any interviews. Fortunately or unfortunately, programming rounds are part of interviews (even for many Data Science related positions) and they often do expect the candidates to think and talk through the problem presented. And depending on the problem it may or may not be possible (as per their requirements) to use language specific approaches. Like your solution.

So, always best to try and think of different ways to solve the same problem. It’s ok if you can’t manage to. It’s a learning process, and a difficult one at that. Reading someone else’s solution is always a good way to learn as well once you have tried to think of it yourself first.

Plus, down the line, you will also learn about improving and optimizing your approaches. You don’t have to worry about that too much now. But just pointing out its relevance since the previous response mentioned it. This aspect becomes important for interviews as well.

1 Like

Slicing is more faster than looping, right?

Could you please explain the behind scene of the slicing code why it is much faster.

Thank you

:heart:

@boemer00
Note that although your code works, it does not express the most intuitive definition of a palindrome.
Your slicing from both directions has left out the last element, because the end index of a slice is excluded.
Yes it still returns True if the input is a palindrome, and you can even reduce the slicing further up to checking only half the string from each direction without affecting the True/False output.

For more concise programming, you can remove if-else, just return dna[0:-1:1] == dna[-1:0:-1]

2 Likes

Hi @boemer00

Even if the autograder checks out your code, it is kind of odd (as @hanqi also pointed out). And there might actually be scenarios, where this could cause issues (e.g. inputs with length 1). This statement dna[0:-1:1] is dropping the last char of the string and is in any case equal to dna[:-1] (start 0 and step 1 is default). The second statement also loses the last (or in this case actual the first) char. I guess it is not thaught at this stage, but a stepping value of -1 inverts a string. Simple solution

def is_palindrome(dna):  
    return dna == dna[::-1]

You can drop the if else clauses (as @hanqi proposed) because the statement itself evaluates to True or False. And the result can in turn be returned directlty.

Best
htw

1 Like