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

Problem with a recursive approach to problem set 2 question 2 (ps3b).

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

Sorry, this question is in regards to problem set 3, not 2

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

that'll be great, thanks

OpenStudy (anonymous):

http://dpaste.com/hold/531832/

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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.

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!
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!