[ create a new paste ] login | about

Link: http://codepad.org/BIxFpQeC    [ raw code | fork ]

Python, pasted on Jan 23:
from string import *

# Recursively putting indexes of the starting point of matches into a tuple and printing them. 

def countSubStringMatchExactRec(target, key, count=(), index = 0):
    if target.find(key) == -1:
        return count
    else:
        return  countSubStringMatchExactRec(target[target.find(key)+len(key):],
            key,
            count + ((index + target.find(key)),),
            index + target.find(key) + len(key))


# Iterative version:

def countSubStringMatchExactIter(target,key):
    index_mover = 0
    count = 0
    positions = ()
    while index_mover < len(target):
        instance = str.find(target, key, index_mover)
        if instance >= 0:
            count += 1
            positions = positions + (instance,)
            index_mover = instance + len(key)
        else:
            break
    return positions


Output:
No errors or program output.


Create a new paste based on this one


Comments: