Hey i just watched lecture for and confused on the syntax he used in the palindrome program. else: return s == 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
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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.
O okay but wouldn't the else need to be an elif or if and not an else because what if s==s[-1] is false how would it no not to return isPalindrome(s[1:-1]) and keep running
The syntax is just dealing with location.
string = 'abcdefg'
string[1:3] = bc
string[1:-1] = bcdef
Try it out in the python shell to get a better grasp of it.
Not the answer you are looking for? Search for more explanations.
I understand the syntax now I kind of just needed a memory jogger on that.
No, you don't need to change it. If s doesn't equal s[-1] it will be false.
a = 4
print a == 4
print a == 5
else: return s == s[-1] and isPalindrome(s[1:-1]) . It says else: return s==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.
or does == automatically work as an if statement?
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
O okay that makes a lot more sense I guess the real thing I didn't understand was the function of the and.
Yeah, it is an inefficient program because it continues with the Recursive function even after a False is called, but oh well.
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.
I would use a elif or an if statement, but both work.