Displaying multiple substring indices within a string by using recursion?

You don't have to implement your own solution, its already done! Use the finditer function from the re module.

The fast way You don't have to implement your own solution, its already done! Use the finditer function from the re module: >>> import re >>> s = 'your code works with wrongly correlated coefficients which incorporates more costs' >>> matches = re. Finditer(s, 'co') >>> positions = match.start() for match in matches >>> positions 5, 29, 40, 61, 77 Your own way If you want to make your own implementation (using recursion) you could take advantage of the extra arguments of the str.

Find function. Lets see what help(str. Find) says about it: S.

Find(sub ,start ,end) -> int Return the lowest index in S where substring sub is found, such that sub is contained within sstart:end. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

There is an extra argument called start that tells str. Find where to start searching the substring. That's just what we need!

So, modifying your implementation, we can get a simple, fast and beautiful solution: def substring_match_exact(target, key, where_should_I_start=0): # Save the result in a variable to avoid doing the same thing twice pos = target. Find(key, where_should_I_start) if pos == -1: # Not found! Return # No need for an else statement return pos + substring_match_exact(target, key, pos + len(key)) What is the recursion doing here?

You're first searching the substring in the string starting at position 0. If the substring wasn't found, an empty list is returned . If the substring was found, it will be returned pos plus all the positions where the substring will appear in the string starting at position pos + len(key).

Using our brand new function >>> s = 'your code works with wrongly correlated coefficients which incorporates more costs' >>> substring_match_exact(s, 'co') 5, 29, 40, 61, 77.

Oh, regular expressions. Well know I know how to do it efficiently, but still any input on my code? I am doing the MIT 6.00 OCW and just read about recursion.

That is why I am focused on doing it by recursion. I know the theory behind the topic, but I want to get this "harder" practical problem done. Factorials and Fibonacci were too easy to make sure I grasp this concept correctly.

– cRaZiRiCaN yesterday 1 @cRaZiRiCaN: recursion is indeed a very interesting concept. Take a look at the other answers to fully understand what was the problem with your solution. – julio.

Alegria yesterday @cRaZiRiCaN I've edited my answer – julio. Alegria yesterday Every basic programming problem can be regarded as being a Homework problem so I understand your reluctance to provide a "solution" (just the mention of an offset approach would of have sufficed too, for example). But thank you very much for the input on the re module.

Did not know about that. EDIT: This was before I saw your edit, thanks again. – cRaZiRiCaN yesterday.

Currently, your code is attempting to find the index of co in the shortened string, rather than the original string. Therefore, while 5, 22, 9, 19, 14 may seem incorrect, the script is doing exactly what you told it to do. By including an offset, like the script below, this code could work.

Def subStringMatchExact(target, key, offset=0): # note the addition of offset if (target. Find(key) == -1): return else: foundStringAt = target. Find(key) target = targetfoundStringAt + len(key): foundStringAt += offset # added return foundStringAt + subStringMatchExact(target, key, foundStringAt + len(key)) # added foundStringAt + len(key) part string = subStringMatchExact("your code works with wrongly correlated coefficients which incorporates more costs", "co") # no need to call w/ 0 since offset defaults to 0 if no offset is given print(string) I should add that making foundStringAt a list from the beginning isn't great practice when dealing with only one value, as you add some overhead with every 0 index lookup.

Instead, since you want a list return type, you should just enclose it in in the return statement (as shown in my code).

Overhead" isn't the issue; complexity is. – Karl Knechtel yesterday.

You are always adding the position in the respective substring. In return foundStringAt + subStringMatchExact(target, key) , the result of the function call is related to the "new" string target which is different from the "Old" one, as it was redefined with target = targetfoundStringAt0 + len(key):. So you should add exactly this value to the function call results: foundStringAt = target.

Find(key) offset = foundStringAt + len(key) target = targetoffset: return foundStringAt + I + offset for I in subStringMatchExact(target, key) should do the trick (untested).

1 for coding a more pythonic answer than mine. – Edwin yesterday.

I wouldn't bother using recursion for this, except as an exercise. To fix the problem: I am having trouble summing the length of the substring on the previous recursion step. What you really want to "sum" is the amount of the string that has already been searched.

Pass this to the function as a parameter (use 0 for the first call), adding the amount of string removed (foundStringAt0 + len(key):, currently) to the input value for the recursive call. As a matter of formatting (and to make things correspond better to their names), you'll probably find it neater to let foundStringAt store the result directly (instead of that 1-element list), and do the list wrapping as part of the expression with the recursive call.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions