[ create a new paste ] login | about

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

Python, pasted on Jan 18:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
keys_in_target = []



def countSubStringMatchRecursive(target, key, count = 0): #Recursive function that counts instances of key in target
                                              # by modifying the target string to include only the letters
                                               # after the point where the key was previously found. 
    location = target.find(key)               # Finds location of key in target.
    if location != -1:
        count += 1                        # Checks to see if key was found.
        keys_in_target.append(target[location:location+len(key)])   # places key in global list.
        return countSubStringMatchRecursive(target[location + len(key):], key, count) # modifies target string to include 
                                                                        #only portion of string after point 
                                                                         #key was found in previous step.
    else: print 'The number of times', key, 'is in the target string', 'is:', count 
     # When key is not found to be contained in string, prints the length of the list of keys. 


countSubStringMatchRecursive('aabbccddeeffggaabbccddeeffggaabbccaaddggdddeeaaddggaabbadddgeagdkeiabbaa', 'aa')


Output:
1
The number of times aa is in the target string is: 7


Create a new paste based on this one


Comments: