I wrote a recursive function to find the number of instances of a substring in the parent string.
The way I am keeping count is by declaring/initialising count
Another way could be to have a third optional parameter on the countSubStringMatchRecursive
function called count that is originally set to 0
. That way you could keep track of the count. This would expose the count variable to the outside world which might not be desirable, but since it's no worse than your global variable I don't think it would be a problem in your case.
You would also have to change the code to make the last recursive call be the call that gives the return statement to the outside world. See this example (untested):
def countSubStringMatchRecursive(target, key, count = 0):
index = find(target, key)
targetstring = target
if index >= 0:
count += 1
target = target[index+len(key):]
countSubStringMatchRecursive(target, key, count)
else:
return "No. of instances of", key, 'in', targetstring, 'is', count
Edit: I realised that you would need a fourth parameter to be able to keep the original string traveling along the recursion. This is probably a less than optimal solution and I would recommend using Greg Hewgill's solution. It has a clean separation between the interactions with the outside and the "business logic", making the code more reusable!
Taking into account overlapping occurrences and maintaining the original definition from MIT this is the simpler and more compact code that I can get.
code:
from string import *
def countSubStringMatchRecursive(target, key):
index = find(target, key)
if index > -1:
return countSubStringMatchRecursive(target[index + 1:], key) + 1
return 0
def test(target, key):
instances = countSubStringMatchRecursive(target, key)
if instances == 0:
print "No instance of %r in %r" % (key, target)
else:
print "Number of instances of %r in %r: %d" % (key, target, instances)
test("atgacatgcacaagtatgcat","ggcc")
test("atgacatgcacaagtatgcat","atgc")
test("banana", "ana")
output:
No instance of 'ggcc' in 'atgacatgcacaagtatgcat'
Number of instances of 'atgc' in 'atgacatgcacaagtatgcat': 2
Number of instances of 'ana' in 'banana': 2
def countSubStringMatchRecursive(target, key):
index = string.find(target, key)
if index == -1:
return 0
else:
return 1 + countSubStringMatchRecursive(target[index + len(key):], key)
Here's something similar to Greg Hewgill's answer. However, instead we pass along the current count each time we call the function, and then return the count when there are no more matches to be made. While I suspect it makes no difference in Python, in languages that implement tail-call recursion, this allows each successive call to do_count
to be optimised away on the call stack. This means that each call to do_count
doesn't cause the call stack to grow.
def count_sub_strings(target, key):
def do_count(target, key, count):
index = target.find(key)
if index >= 0:
target = target[index + len(key):]
return do_count(target, key, count + 1)
else:
return count
return "No. of instances of %s in %s is %s" % (key, target, do_count(target, key, 0))
How about this?
def count_it(target, key):
index = target.find(key)
if index >= 0:
return 1 + count_it(target[index+len(key):], key)
else:
return 0
print count_it("aaa bbb aaa ccc aaa", "aaa")
Output:
3
One way to modify your code would be to use a local function as follows:
def countSubStringMatchRecursive(target,key):
def countit(target,key,count):
index=find(target,key)
if index>=0:
target=target[index+len(key):]
count += countit(target,key,count) + 1
return count
return "No. of instances of", key, 'in', target, 'is', countit(target,key,0)