Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

I am working on ps3 and they're asking for two functions to iteratively and recursively count the number of matches of the key in the target. This seems too obvious for part a: def countSubStringMatch(target,key): print target.count(key) is this correct? and then secondly, I don't have any idea how to do it recursively. I tried to write a recursive function that would tell me exactly where the key is hiding in the target but it's not working:

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

def countSubStringMatchRecursive(target,key): y = find(target,key) print y while y!= -1: countSubStringMatchRecursive(target[y+1:len(target)],key)
Yes, the iterative function is that easy. I've been stuck on exactly the same problem with making it recursive. I actually spent an embarrassing amount of time weeding through ancient posts in this group, and I dug up the 4 codes on the link below. And just for kicks, I added your code, too. I modified the whole thing so that each one will print out some results so I can figure out what the heck they each did. I'm sure several other study groupies will recognize their own code: http://dpaste.com/hold/612083/ You will notice that the second one doesn't give the same results as #1, 3, and 4. Function 5 is your code (which seems to have an infinite loop in it, although it reports the correct answer as the second value). Because I don't quite understand how to make this problem recursive, I can't tell you why any of these 5 examples do or don't work. I'm kinda hung up on "from string import *" right now. Something else helpful I dug up in ancient questions was this: The assignment is to write functions for how many times the key occurs in a target. We're not asked to report the indices of those matches (which I spend a few days trying and failing to do). Please, if you figure it out, post it!
In my dpaste post, most everyone did this: return 1 + countSubStringMatchRecursive(target[nextindex:], key) I don't understand why they added one to the re-call of their functions. And BTW, this seems to be what tmd197's function is missing.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

Julie, do you know what people mean by basecase?
the base case does not make a recursive call.
That's right; it's what stops the recursion. If you have a function that calls itself: def f(): return f() When you call it, `f()`, the interpretor will stop executing the code around the function call and start executing the function definition. Which requires the interpretor to stop executing that definition, create another instance of `f`, then execute that, and so on, and so on, You'll just get loads of instances of the function, but they'll never return anything. So it'll be infinite recursion all the way. You need a basecase in the definition to stop it at some point. In order for that to happen, the argument passed will need to change with each new instance...
Take the following function: def f(n): if n == 2: return 2 else: return n * f(n - 1) Have a look at it, I'll try and explain...
Isn't that what this does: countSubStringMatchRecursive4(target[target.find(key)+1:] I get adding one inside the index. The function that worked in my dpaste was: countSubStringMatchRecursive4(target[target.find(key)+1:] + 1 Why add one to the whole thing? Wouldn't that give you the index + 1, which would then be one more than the correct answer?
If I call it, passing, lets say 3: f(3) because 3 is not equal to two, the else part will be executed. That'll create a new instance of `f`, passing n - 1, which would be 2. This will also prevent the first instance from returning anything for now. The new instance, passed 2, will return 2, because that's what it does whenever it's passed 2. It returns 2 to the first instance, which is still waiting to know what f(n -1) is. It is 2, so the first instance returns n * 2, or 3 * 2, which is 6 of course. If I pass a bigger number, say 100, it'll just take longer, as it'll spawn a long chain of instances of the function `f`, each waiting for the one that gets passed 2, to return 2, allowing it to reel back in, garbage collecting each instance of the function as it returns, so that, eventually, the first one called, can return 100 * 99 * 98 * 97 ... 3 * 2
f(4) return 4 * f(3) f(3) return 3 * f(2) f(2) return 2
4 * 3 * 2
I can't read the code you posted on dpaste, the links dead.
Thank you, carlsmith. You are a very patient man! Yes, I understand why you want to add one to it. But it seems like the successful codes in my dpaste added one to the index AND one to the whole thing. That seems redundant.
Ah, see, I can't read the code. I'm not sure what it's meant to do. This line... countSubStringMatchRecursive4(target[target.find(key)+1:] should throw a syntax error, as should... countSubStringMatchRecursive4(target[target.find(key)+1:] + 1 They both, lack a closing paren.
Sorry, Julie, you'll have to post the code again mate.
Hmmm... don't know where it went. Let's try that again: http://dpaste.com/hold/612100/
I'll have a look now.
Which one works?
#1, #3, and #4
k
The third one has a one one the end, because it doesn't have it at the front of the expression. return countSubStringMatchRecursive4(target[target.find(key)+1:],key)+1 return 1 + countSubStringMatchRecursive4(target[target.find(key)+1:],key) The 1 represents the match that must have been found if the else block is being executed.
It's pretty ugly code to be honest. You should use nicer names for things, don't listen to the geeks. def count_matches(string, substring): if string.find(substring) == -1: return 0 else: return 1 + count_matches(string[ string.find(substring)+1: ], substring) Try and keep it clean and tidy.
Shame I can only give you one medal :*
Thank you. Have a go at it, but if you want to learn recursion, which, to be honest, isn't that useful very often, you'd still be better of starting with numbers, little ones. Iterating around the place at the same time, just make's it too confusing. My function above will take the number of cards in a deck and return the odds to one of the deck being shuffled and ending up in some predetermined order. For a regular deck, the odds are simply 52*51*50...4*3*2. That's all it does, but it shows how recursion works without many moving parts.
Let me know if you're still stuck. I skipped recursion when I first started and came back to it. It's painful at first.
Recursion is a neat concept, but it isn't suitable for really deep applications. A quick test shows: def recurse_check(calls): calls[0] += 1 recurse_check(calls) calls=[0] try: recurse_check(calls) except RuntimeError: print 'Number of calls:',calls[0] Output: Number of calls: 999
of all the things they could have taught in an intro class, i wonder why they decided to include recursion?
I'm glad to hear it isn't a simple concept. I find iteration WAY more intuitive. Oh, and sorry to tmd197 for hijacking your question ;) I'm hoping all this chatter was helpful for you, too.

Not the answer you are looking for?

Search for more explanations.

Ask your own question