A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    or does == automatically work as an if statement?

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Ok thanks

  14. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.