anonymous
  • anonymous
Base case for recursion? I understand the concept of recursion, but for Problem Set 3 problem 1 part b I'm having a hard time coming up with a base case. Any hints? Thanks.
MIT 6.00 Intro Computer Science (OCW)
schrodinger
  • schrodinger
See more answers at brainly.com
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

anonymous
  • anonymous
how about "if the keystring is NOT in the targetstring/current section of the targetstring, return 0/something else that lets you stop calculating"
anonymous
  • anonymous
I have something that works, but I can't figure out how to print out a number of instances. I can just print out 'one instance' per time the key is found. How can I print out the actual number of instances? def countSubStringMatchRecursive(target, key): index = 0 target_length = len(target) if find(target[index:target_length], key) == -1: # test to see if value is returned return 0 else: position = find(target[index:target_length], key) index = position + 1 print "one instance" return countSubStringMatchRecursive(target[index: target_length], key) I tried to return a counter variable that increments, but when i print it it always stays at 0. Thanks in advance. def countSubStringMatchRecursive(target, key): index = 0 target_length = len(target) counter = 0 if find(target[index:target_length], key) == -1: # test to see if value is returned print counter return 0 else: position = find(target[index:target_length], key) index = position + 1 counter += 1 print "one instance" return countSubStringMatchRecursive(target[index: target_length], key) and counter
anonymous
  • anonymous
I think your problem lies in your IF statement. I think you need to use a WHILE loop...otherwise your code will choose one of your two paths, execute, then end. The while loop will look until your counter max is achieved.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
You can also try a FOR loop, as I did in my code. It works, but you have to have the right conditions. Make sure your conditions can be met, and make sure that it will iterate or recur for all instances of your key.
anonymous
  • anonymous
I'm honestly still not that happy with my solutions to this problem set, so someone should jump in if I start talking nonsense. Basically, for this problem I think you need another parameter besides target and key. It would be a 0 int or an empty or nearly-empty list, and it would get iterated at each recursion. The pseudocode would look like: recursionfunction(target,key, counter): if the keystring is not in the targetstring: return the counter else: add one to the counter location = where it was found return the result of the recursionfunction with a shortened targetstring and the current key and counter
anonymous
  • anonymous
Try this. No extra counting parameter needed. def countSubStringMatchRecursive(target, key): found_at = target.find(key) if found_at >=0: return 1+ countSubStringMatchRecursive(target[found_at+1:], key) else: return 0 # not found
anonymous
  • anonymous
Yeah, that's better. Thanks!

Looking for something else?

Not the answer you are looking for? Search for more explanations.