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

pset3 problem 1 countSubStringMatch Hello Please feel free to criticize the http://codepad.org/XqAu8VRz Are there any other approaches for writing iterative function for counting substrings in pset3 problem 1

OpenStudy (anonymous):

you can try something like find(target[zz+1:], key) this will only search the target string from the previous 'zz' onwards

OpenStudy (anonymous):

I don't think this function works. I tested it using "abababab" and "ab" and it returns 0 instead of the expected 4. I don't understand how to figure this program out without using a string length function (are we allowed to use it) Your code is built to stop when a match isn't found - which will usually happen on the first iteration target/key pairs.

OpenStudy (anonymous):

@rcavezza: I tried this code as well and did not get it returning '0' in this instance. It correctly returns '4'. @den2042: So, basically, this is a great, concise piece of code. More concise than my version of it, which I've added here for you to look at. I essentially add in more variables and conditional statements to do what your fewer variables do just as well. It's only here for you to see another way of doing it, but I would advise against my way. The only other comment I have is similar to your recursive version, in that you can use 'zz + len(key) + 1' instead of 'zz + 1' to cut down on some search time. Great job.

OpenStudy (anonymous):

Also, @pepperpot u r right as well - good additional suggestion on cutting down run time. Also also, @den2042, here's the actual code I promised: http://codepad.org/ru5ZxEY0

OpenStudy (anonymous):

I find @den2042's solution the most elegant in terms of concision and readability. Here's a variant approach that uses slicing. The basic idea is that when we find a match we slice off the front part of the string and create a new target string: http://codepad.org/uBu2b4Xl

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!