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

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

@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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

http://codepad.org/a0BTzuS0

OpenStudy (anonymous):

yeah. that makes sense. i don't know why i didn't think of that. 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!
Latest Questions
Breathless: Spooky witch but cute
6 hours ago 3 Replies 0 Medals
Arriyanalol: help
5 hours ago 10 Replies 2 Medals
Arriyanalol: @tinydinoUwU stop trying to find a argument u blad lil boy
1 day ago 5 Replies 4 Medals
Jaded012023: Please tell me what you all think of this song
8 hours ago 6 Replies 1 Medal
Arriyanalol: bro how
8 hours ago 2 Replies 3 Medals
Arriyanalol: cant wait for the new bluey movie in 2027
1 day ago 12 Replies 2 Medals
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!