Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

student47

pset3 problem1. sos. i can't make recursive one can anybody give me some hint please?

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. student47
    Best Response
    You've already chosen the best response.
    Medals 0

    oh i just have got an answer!

    • 2 years ago
    1 Attachment
  2. maitre_kaio
    Best Response
    You've already chosen the best response.
    Medals 4

    The two functions should return the number of occurrences of key in target. You can check my version here: http://codepad.org/qz73jpja I also joined two diagrams to help you visualize the code. Let's review first the code of your iterative version. My first objection is that you always return None, even if you print occurrences when you find them. You should have a variable which counts how many times you find the key. Now you iterate over the target, starting with position 0, if you find the key at position n, increment your counter and search again starting at position n+1. As soon as you don't find the key, return the number of occurrences you have found. Now let's look at the recursive version. You did pretty well and almost found the answer, since you call the same function with a shorter target (hence it is a simplified version of the same problem). You should only ask yourself: what do I want to return when I don't find the key -> you should return 0. And when I find the key? -> 1 + return from the shorter target. I hope I made it clear, but let me know if it is not enough !

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

    very helpful advice! thank you!

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

    maitre_kao. nice job. elegant. i like the way you increment directly in the return statement.

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

    here is another variation http://dpaste.com/685648/

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

    bwCA. very cool and concise. didn't know about "not in"

    • 2 years ago
    • Attachments:

See more questions >>>

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.