PS3 Ok i think i finished it problem 1 and 2. Here is the code: http://dpaste.com/573289/ http://dpaste.com/573287/ Allthough on problem 1 on the recursive method i eventualyl got a bit of help from a friend. I did get the idea but as he said as well i was allways messing up the syntax.For example if u check the code i have commented out 2 lines which without help i dont think id have found how to code them right. Also the recursive method it doesnt seem to do 100% whats its asked in the definition. Its supposed to COUNT the number of instances of key in target,not point the indexes its found
I guess i have to create a list/tuple of index and someone how count it or something like that? But again im not quite sure how im supposed to do that even without loop. Again i think im confused here hehe.
god damn typos.... indexes*,somehow*
I had problems with 3b as well. How do you carry a count through recursion? There are ways to do it...you can add another argument (I think I did that too in the end...its kind of cheating:) and use it to keep your count. Another thing I have seen people do is add another function so they keep the arguments specified in the problem, and from that function call a function with more arguments...something like this.. def SubRec(target, key): blah blah return SubRecHelper(target, key, count) def SubRecHelper(target, key, count): blah blah return count For me the easiest way to deal with the problem is with the use of "global" variables. Outside the function (like down at the bottom where you call the function) you set a variable, for instance "count=0". Then, inside your recursive function you refer to that variable like this.. def SubRec(targ, key): global count count=count+1 This keeps the count independent of your recursion, and you can still return this count when you are done. There are probably other ways to do it as well that I don't know. ********** On another subject, I would suggest cutting your target down before sending it back through recursion rather than specifying a starting point. I did this by adding a fake string to the target at the end (in case the last letter was the target) and....new target= target[target.find(key)+1:]
**edit**(in case the last letter was the KEY) -like this: target+='@' (assuming your key isn't '@')
Join our real-time social learning platform and learn together with your friends!