codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
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')
Private
[
?
]
Run code
Submit