A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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
anonymous
 5 years ago
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

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0you can try something like find(target[zz+1:], key) this will only search the target string from the previous 'zz' onwards

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Also, @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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.