Problem 1 of Problem set 3: for the recursive function, I got my code working by writing a function within a function, it's quite long compare to the iterative method. My question is: is there a way to write the recursive function using only one function?

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

Problem 1 of Problem set 3: for the recursive function, I got my code working by writing a function within a function, it's quite long compare to the iterative method. My question is: is there a way to write the recursive function using only one function?

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

Yes, there's definitely a way to do that. My original solution used an extra argument to count occurrences, but polpak had a cool solution that just used the key and target strings.
Sure, but the recursive solution doesn't really buy you much in terms of readability over the iterative one (which IMO is the only reason you'd want to use recursion). Additionally, to make a truely recursive solution you have to take a third argument (possibly defaulted to 0) to tell the sub-calls where to start their search. e.x. def subStringMatchExact(target,key,position=0): index = target.find(key,position) if index != -1: return (index,) + subStringMatchExact(target,key,index + len(key)) else: return ()
once again the formatting of the code has been munged by the silly javascript interface on this website

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

I'm pretty sure that's just the 'counting matches' problem, for which your solution was on the order of: index = find(target,key) if index == -1: return 0 else: return 1 + countsubstringmatchrecursive(target[index+len(key)], key)
I remembered because I hadn't thought of doing it that way, and I was stuck with an extra argument too.

Not the answer you are looking for?

Search for more explanations.

Ask your own question