Advertisement
xavicano

Untitled

Aug 26th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import  random
  2. from random import randint
  3.  
  4. def random_char(num_of_char):
  5.     charset="".join(chr(x) for x in range(48,122)) 
  6.     #return charset[random.randint(0,74)]
  7.     password="".join(charset[random.randint(0,73)] for y in range(num_of_char))
  8.     return password
  9.    
  10. ##Example 1 - A while loop
  11. #position = 0
  12. #while position < len(sequence):
  13.     #print(sequence[position], "is at position", str(position))
  14.     #position += 1
  15.  
  16.  
  17. ##Example 2 - A for loop over a range
  18. #for position in range(len(sequence)):
  19.     #print(sequence[position], "is at position", str(position))
  20.  
  21.  
  22. ##Example 3 - The enumerate function
  23. #for position, item in enumerate(sequence):
  24.     #print(item, "is at position", position)
  25. def search_for_enumerate(list_to_search, target, is_sorted):
  26.     for position, item in enumerate(list_to_search):
  27.         if target==item:
  28.             print(target, "is at position", position)
  29.         elif is_sorted and position > len(list_to_search):
  30.             print (" Taget not found in sorted list")
  31.             return False           
  32.         elif position >= len(list_to_search)-1:
  33.             print (" Taget not found in unsorted list")
  34.             return False
  35.     return True
  36.        
  37. def binary_search(sorted_list, value, direction, around):
  38.     if around==True:
  39.         value=round(value)
  40.     if direction=="left":
  41.         left = 0
  42.         right = len(sorted_list) - 1
  43.         while left <= right:      
  44.             mid = int((right + left)/2)             # mid to int between left and right      
  45.             if sorted_list[mid] > value:            # if sorted_list[mid] > value  set right to mid
  46.                 right = mid - 1      
  47.             elif sorted_list[mid] < value:          # if sorted_list[mid] < value  set left to mid
  48.                 left = mid + 1      
  49.             else:                                   # if sorted_list[mid] == value  return mid
  50.                 return mid                                         
  51.         return False                                # loop ends return `False`
  52.     elif direction=="right":
  53.         right = 0
  54.         left = len(sorted_list) - 1
  55.         while left >= right:      
  56.             mid = int((right + left)/2)             # mid to int between left and right      
  57.             if sorted_list[mid] < value:            # if sorted_list[mid] > value  set right to mid
  58.                 right = mid - 1      
  59.             elif sorted_list[mid] > value:          # if sorted_list[mid] < value  set left to mid
  60.                 left = mid + 1      
  61.             else:                                   # if sorted_list[mid] == value  return mid
  62.                 return mid                                         
  63.         return False                                # loop ends return `False`
  64.        
  65.  
  66.  
  67. list_of_num = [randint(0,50) for i in range(100)]
  68. print(list_of_num)
  69. list_of_char = random_char(50)
  70. print(list_of_char)
  71.    
  72. search_for_enumerate(list_of_num, 50, False)   
  73.  
  74. search_for_enumerate(list_of_char, "a", False)
  75.  
  76. list_sorted=sorted(list_of_num)
  77. print(list_sorted)
  78.  
  79. print("First value found by left", binary_search(list_sorted, 15, "left"))
  80.  
  81. print("First value found by right", binary_search(list_sorted, 15, "right"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement