Problem with a recursive approach to problem set 2 question 2 (ps3b).
I always end up with the function yielding nested tuples, plus I could not do it without passing an additional arg. to the function to keep the length of the sliced string. http://dpaste.com/531825/ Any ideas?
Sorry, this question is in regards to problem set 3, not 2
I don't understand recursive functions enough to explain them but I can give you mine if you want it and you can try to figure it out?
that'll be great, thanks
to solve the nested tuple problem, instead of returning loc, subStringMatchRecursive(target,key,(loc+1)) you return things like loc + subStringMatchRecursive(target,key,(loc+1)) when you use a comma, even without parens it assumes your trying to make a tuple, or at least it does sometimes, its sketchy at best. when you use the "+", tried to create one whole thing. sometimes the tuple doesn't like to add with a non tuple, when that happens i find that you can help it along by putting the non tuple variable (say x) tuple + (x,) hopefully that made sense and i understood what was going wrong
Like Nessman said, you can fix your code by returning: (loc,) + subStringMatchRecursive(target,key,(loc+1)) You'll also have to seed an empty tuple instead of None, which is what you're implicitly seeding (by not having a return outside the while loop). You can do that by adding "return tuple()" outside your while loop. I've attached my ps3b.py for reference. I solved it three different ways, but my "subStringMatchExact2" is closest to your code.
Join our real-time social learning platform and learn together with your friends!