Search
 
SCRIPT & CODE EXAMPLE
 

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"))
Comment

PREVIOUS NEXT
Code Example
Python :: json object type in python 
Python :: pandas concatenation (concat) using list comprehension 
Python :: Boolean comparison w/out if statements 
Python :: pylance not reading django 
Python :: change y axis scale python 
Python :: twitter python 
Python :: how to reassign a key py 
Python :: python getting line length using list comprehension 
Python :: python Access both key and value using iteritems() Return keys or values explicitly 
Python :: jax.numpy 
Python :: python map and filter 
Python :: non venomous snakes 
Python :: spotify meist gespielte lieder sehen 
Python :: dalsports 
Python :: python bill 
Python :: how to create a scoreboard for the top 5 players in python 
Python :: python initialize a 2d array 
Python :: find location of a class in python 
Python :: python messaging networking 
Python :: dictionnaire 
Python :: python 3.10.5 release date 
Python :: python update python 
Python :: how to save a count countvectorizer model in python 
Python :: repeating a program in python 
Python :: pypi cryptography 
Python :: dataframe corr p value 
Python :: Python RegEx re.compile() Set class [s,.] will match any whitespace character ‘,’ or ‘.’ 
Python :: python datediff days 
Python :: access dictionary in f string 
Python :: sanic ip whitelist 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =