Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

Hey i just watched lecture for and confused on the syntax he used in the palindrome program. else: return s[0] == s[-1] and isPalindrome*(s[1:-1])*I don't understand the starred portion here is link to complete script if needed. http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/video-lectures/lecture-4/lec4.pdf

OpenStudy (anonymous):

What it does is create a recursive function that calls on itself without the first letter and the last letter. isPalindrome('racecar') would check to see if the first letter of the word and the last letter of the word are the same. Then it would call on isPalindrome again, but with 'aceca'. Whenever the length is less than or equal to one it'll return true.

OpenStudy (anonymous):

O okay but wouldn't the else need to be an elif or if and not an else because what if s[0]==s[-1] is false how would it no not to return isPalindrome(s[1:-1]) and keep running

OpenStudy (anonymous):

The syntax is just dealing with location. ex: string = 'abcdefg' string[1:3] = bc string[1:-1] = bcdef etc... Try it out in the python shell to get a better grasp of it.

OpenStudy (anonymous):

I understand the syntax now I kind of just needed a memory jogger on that.

OpenStudy (anonymous):

No, you don't need to change it. If s[0] doesn't equal s[-1] it will be false. ex: a = 4 print a == 4 print a == 5

OpenStudy (anonymous):

else: return s[0] == s[-1] and isPalindrome(s[1:-1]) . It says else: return s[0]==s[-1] where in the syntax does it tell you only if it is true to continue. I thought else doesn't work the same as an if statement where it only continues if a statement is true. I thought else will go through all commands in it's parameters.

OpenStudy (anonymous):

or does == automatically work as an if statement?

OpenStudy (anonymous):

An elif would work, but this program function because the 'and'. If it returns True and True, it'll work, however if at one point in the recursive function it returns False, then at the very beginning it will be True and False and return False overall. True and True = True True and False = False

OpenStudy (anonymous):

O okay that makes a lot more sense I guess the real thing I didn't understand was the function of the and.

OpenStudy (anonymous):

Yeah, it is an inefficient program because it continues with the Recursive function even after a False is called, but oh well.

OpenStudy (anonymous):

So which would be better using an elif or if to make sure it stops when one part is false or the way he did it and let the recursive go through and see that it was false at the end.

OpenStudy (anonymous):

I would use a elif or an if statement, but both work.

OpenStudy (anonymous):

Ok thanks

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!