A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

PS3,Q1. I am trying to write a recursive function that will find the number of times one string (key) is located in another string (target). It's a simple problem, but I am wondering how to count the number of times the key has been found in the target without using a global variable. (i.e. Is is possible to keep a count of how many times a recursive function has run without using a global variable?) http://codepad.org/Fh5OS8CL

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

    You can pass a hidden parameter to the function, namely count, which defaults to 0, then increment it, and keep calling the function with the new count. Check: http://codepad.org/JY3D31c1

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

    Interesting. Didn't know you could pass a hidden parameter. Thanks for the help.

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

    here is one way to count things recursively http://pastebin.com/J0VqcGCF

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

    @bwCA: Thanks for the tip. But how do I get the output of the function to print? I know I could define a second function that simply prints what is returned in the count_letters function, but is there another way to do it? http://codepad.org/4r7j0sn5

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

    Nvmd. Codepad doesn't print a return command, but python shell does. I wonder if I'm doing something wrong on codepad.

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

    http://codepad.org/a0BTzuS0

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

    yeah. that makes sense. i don't know why i didn't think of that. thanks.

  8. 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.