January 28th, 2013, 02:05 PM
A quick easy one
Nevermind ... I'll leave the question for a chuckle. However, it's because 'is_palindrome' should have a Return in front of it.
Silly code, just doing as it was told to.
Can someone please explain this insanity to me.
if len(s) == 0:
print 'i was called'
if s[0:1] == s[(len(s)-1):len(s)]:
i was called
I know, I know, your thinking . . . . well of course this should return True. However, you must remember this is programming a lot of times things just don't make sense. So in this case instead of returning True it ignores that and decides it's going to go with None.
And you get a gold star if you can tell me how you would code it so when it gets to line that says "Return True" it ......
wait for it.
January 28th, 2013, 03:07 PM
# Recursion is a poor idea
# testing is a good idea.
# this program is non-recursive and untested.
# you decide.
L = list(s)
return L == list(reversed(L))
[/code] are essential for python code and Makefiles!
January 28th, 2013, 03:53 PM
Tested and very simple ...
Improvements can be lower case and letters only.
return s == s[::-1]
print(is_palindrome('racecar')) # True
Comments on this post
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25