[ create a new paste ] login | about

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

morgkl6 - Python, pasted on Jan 20:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def subStringMatchExact(target, key):  # Iterative function that prints the location(s) of a key string in a target string as a tuple.
	isFound = True                              
	location = 0                                     # Initial starting point (first item of target string)
	x = 0                                                  # x is the location where key is found in target
	listOfLocations = []                         # list of locations found
	while isFound:                                 
		x = target.find(key, location)      
		if x != -1:                                         # check if key was found (x will be -1 if not found)
			listOfLocations.append(x)    # add found location of key in target to list
			location = x + 1                       # search for key again after moving starting point one item further in target string
		else: isFound = False                       
	print tuple(listOfLocations)                       # when key is not found, convert list of found locations into a tuple and print
        return None      


subStringMatchExact('atgaatgcatggatgtaaatgcag', 'atg')


Output:
1
(0, 4, 8, 12, 18)


Create a new paste based on this one


Comments: