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)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
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.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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.
index = target.find(key,position)
if index != -1:
return (index,) + subStringMatchExact(target,key,index + len(key))
Not the answer you are looking for? Search for more explanations.
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 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.