SHARE
TWEET

Untitled

a guest Aug 19th, 2019 68 in 330 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def search4it(list_in, wanted, is_sorted=False):
  2.     # return position (>= zero) found at, or negative for not found
  3.     for pos, val in enumerate(list_in):
  4.         if val == wanted:
  5.             return pos
  6.         elif is_sorted and wanted < val:
  7.             return -2 # just to get a different code than exhausting list
  8.     # not found if it gets here
  9.     return -1 # position zero (1st item) also tests as False
  10.                                                                      
  11.  
  12. royalboys=['harry','charles','edward','philip','william','andrew']    
  13. royalboys.sort()                                                      
  14.  
  15. print('harry found at ',search4it(royalboys,'harry'),
  16.       search4it(royalboys,'harry',True))            
  17. print('andrew found at ',search4it(royalboys,'andrew')
  18.       ,search4it(royalboys,'andrew',True))
  19. print('zara found at ',search4it(royalboys,'zara')
  20.       ,search4it(royalboys,'zara',True))
  21. print('john found at ',search4it(royalboys,'john')
  22.       ,search4it(royalboys,'john',True))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top