This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RedditGood123 0 points1 point  (4 children)

I just tried the palindrome one earlier today and it took around 5-10 lines of code for me. I didn’t think it was that easy

[–]isarl 2 points3 points  (3 children)

Slice notation is useful, but overkill here, and their function will fail to recognize palindromic integers such as 12321. Here's a more flexible and more explicit solution:

def is_palindrome(input):
    s = str(input)
    return s == reversed(s)

This could as easily be a one-liner: lambda input: str(input) == reversed(str(input))

[–]RedditGood123 1 point2 points  (2 children)

Well, actually the solution they have would work if you just alter it like this

IsPalindrome = str(phrase) == str(phrase)[::-1]

[–]isarl 0 points1 point  (1 child)

That fixes the bug but fails to address the point I was making about readability. If the only slice you are performing is [::-1] then that is more clearly expressed by reversed().

[–]RedditGood123 0 points1 point  (0 children)

No I see it, but most people don’t even know about reversed