Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Here is the code for the Sequential or linear search:
- """
- target = 2
- # Creates a variable poistion and assinging it a value of 2
- lyst = [4, 3, 2, 1]
- # create a variable called list and assiging it list
- # Create a function call sequentailSearch that accepts two input parameters, target and lyst
- def sequentialSearch(target, lyst):
- """Returns the positon of the target item if found or -1 otherwise"""
- position = 0
- #Creates a variable calle position and assigning it value of 0
- # Creatinge a while loop that checks if position which is 0 is less than
- # length of the list which is a true statment because lenght of the list is 4 and
- # position is 0
- while position < len(lyst):
- # Comparing the target is equal to lyst[position] the value at list of positon
- # return the position
- if target == lyst[position]:
- # return the positon
- return position
- position += 1
- # Increase the position by one
- return -1
- # return -1 at th end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement