pset3 problem 1
Please feel free to criticize the
Are there any other approaches for writing iterative function for counting substrings in pset3 problem 1
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!
you can try something like find(target[zz+1:], key)
this will only search the target string from the previous 'zz' onwards
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.
@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.
Not the answer you are looking for? Search for more explanations.
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:
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: