Question about ps3: I'm stuck on the function that recursively counts how many instances of the key are in the target string. Here is the code I have so far: http://codepad.org/uzMMuLbL I basically just need to count how many times it runs the "else" block of code, but I can't figure out a way to do this. I used a while loop for the iterative function. I'm not sure if I can use one again while keeping the function recursive. any help/hints are appreciated.
You get an error on the rcount because it is a variable used outside of the definition that you are trying to access in the definition. In order to access it inside the definition you will need to add: "global rcount" after the definition declaration. Example: rcount = 0 def countSubStringMatchRecursive(target, key): global rcount
This will work well with incrementing it and it will not reset it to 0 each time.
thanks a lot man! that's exactly what I was looking for
No problem :] that one bugged me a while back.
Another way to code it (and somewhat better, since using global variables is frowned upon), is to declare countSubString..(target, key, count = 0) and then increment on the program and passing it as argument. Works fine, and it's somewhat more elegant, IMO. Using globals is kind of a kludge.
There are many different ways that people would want to do things like this. True you would not really want to use a counter as a global variable but if that variable was something other than a counter and was more important then you can use a global. Another way of doing this if you were not using a counter would be to pass the variable in and then use it as the return variable. rcounter = 0 def count(passedCounter): return passedCounter + 1 rcounter = count(rcounter) print rcounter
Well, it's a counter, and being one, it's somewhat of bad design to have one set as global, especially in a language like Python that is both modulated and weakly-typed. Strange stuff could happen if you import some modules in a main (interface) program and it existed both counter, one global and one local. Global variables, pretty much as the goto statement in C, are something that you almost never use, or at least, avoid as much as possible, especially for bigger projects. (Albeit for different reasons)
oh i agree :] don't think I don't. the full example of what bmp was saying would be def counter(count = 0): print count if (count != 5): counter(count + 1) counter()
The way I was thinking was more like countSub...(string, target, count = 0): if (find (string, target) == -1): #reduce string for recursive step count += 1 countSub..(string, target, count) but, yeah, anything like that works fine.
thanks for all the help, I feel like I'm learning more reading conversations between people who know what they're doing than by reading pages of documentation. bwca, thanks for the example, I couldn't think of a way to work it out without a counter
very good...
Join our real-time social learning platform and learn together with your friends!