Advertisement
karlakmkj

Linear search sample

Jan 7th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. #First, we declare the function:
  2. def linear(a_list, an_item):
  3.    
  4.     #Then, we iterate through the list:
  5.     for i in range(len(a_list)):
  6.        
  7.         #If we've found the item, we're done! Go ahead and return this index.
  8.         if a_list[i] == an_item:
  9.             return i
  10.        
  11.     #If we reach the end of the function it means we never returned an index, which means we never found the item and can return False.
  12.     return False
  13.  
  14. a_list = [5, 1, 3, 6, 7, 3, 1, 6, 7, 8, 3, 6]
  15. print(linear(a_list, 6))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement