Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. """
  2. Here is the code for the Sequential or linear search:
  3. """
  4.  
  5. target = 2
  6. # Creates a variable poistion and assinging it a value of 2
  7. lyst = [4, 3, 2, 1]
  8. # create a variable called list and assiging it list
  9.  
  10. # Create a function call sequentailSearch that accepts two input parameters, target and lyst
  11. def sequentialSearch(target, lyst):
  12. """Returns the positon of the target item if found or -1 otherwise"""
  13. position = 0
  14. #Creates a variable calle position and assigning it value of 0
  15. # Creatinge a while loop that checks if position which is 0 is less than
  16. # length of the list which is a true statment because lenght of the list is 4 and
  17. # position is 0
  18. while position < len(lyst):
  19. # Comparing the target is equal to lyst[position] the value at list of positon
  20. # return the position
  21. if target == lyst[position]:
  22. # return the positon
  23. return position
  24. position += 1
  25. # Increase the position by one
  26. return -1
  27. # return -1 at th end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement