Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

needle in haystack

def search(needle, haystack):
    firstFound = -1
    for x in range(0, len(haystack)):
         if(haystack[x] == needle[0] and firstFound == -1):
             firstFound = x
             for y in range(1, len(needle)):
                 if(needle[y] != haystack[x+y]):
                     firstFound = -1
                     break
#trick is you need to make sure you have switched firstFound back to -1 if previous attempts(only partially found) have failed
    return firstFound
print(search("a", "xxxx"))
 
PREVIOUS NEXT
Tagged: #needle #haystack
ADD COMMENT
Topic
Name
3+1 =