Advertisement
colinm86

Untitled

Oct 29th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. def time_decorator(fn):
  5.     """Decorator function that when added to function returns execution time
  6.        as well as function return val
  7.    Arguments: fn {function}
  8.    Returns: x (function return val) , time (Execution time)
  9.    """
  10.     def foo(lst,item):
  11.         start = time.time()
  12.         x = fn(lst,item)
  13.         end = time.time()
  14.         return x, end-start
  15.     return foo
  16.  
  17. def time_decorator_2(fn):
  18.     """Decorator wrapper function that times functions with 3 parameters and one return val
  19.    Arguments: fn {function}
  20.    Returns: x (function return val) , time (Execution time)
  21.    """
  22.     def foo(alist,first,last,item):
  23.         start = time.time()
  24.         x = fn(alist,first,last,item)
  25.         end = time.time()
  26.         return x, end-start
  27.     return foo
  28.  
  29.  
  30. def binarySearch(alist, item):
  31.     """Non recursive binary search algo
  32.    Arguments:
  33.        alist {List}
  34.        item {Object}
  35.    """
  36.     first = 0
  37.     last = len(alist)-1
  38.     found = False
  39.     while first<=last and not found:
  40.         midpoint = (first + last)//2
  41.         if alist[midpoint] == item:
  42.             found = True
  43.         else:
  44.             if item < alist[midpoint]:
  45.                 last = midpoint-1
  46.             else:
  47.                 first = midpoint+1        
  48.     return found
  49.  
  50.  
  51. def rec_bin_search(alist, item):
  52.     """Recursive binary search algo
  53.    Arguments:
  54.        alist {List}
  55.        item {Object}
  56.    """
  57.     if len(alist) == 0:
  58.         return False
  59.     else:
  60.         midpoint = len(alist)//2
  61.         if alist[midpoint]==item:
  62.             return True
  63.         else:
  64.             if item<alist[midpoint]:
  65.                 return rec_bin_search(alist[:midpoint],item)
  66.             else:
  67.                 return rec_bin_search(alist[midpoint+1:],item)
  68.  
  69. '''
  70. Exercise # 1
  71. Set up a random experiment to test the difference between a sequential search and a
  72. binary search on a list of integers. Use the binary search functions given in the text
  73. (recursive and iterative). Generate a random, ordered list of integers and do a benchmark
  74. analysis for each one. What are your results? Can you explain them?
  75.  
  76. Results: The recursive algorithm grows exponentially over time where the non recursive algorithm
  77. stays roughly the same. I belive this is due to the slice operators that are O(n)
  78. '''
  79. def time_test_binary_search():
  80.     """Test recursive binary search function vs non recursive binary search function.
  81.    Must add time decorators to functions before running test
  82.    Returns: {String} -- Time results for 2 algorithms
  83.    """
  84.     lst = [i for i in range(10000000)]
  85.     item = random.randint(0,10000000)
  86.     x,y = rec_bin_search(lst,item)
  87.     a,b = binarySearch(lst,item)
  88.     return f'Recursive Time:{y}, Non Recursive:{b}'
  89.  
  90.  
  91. '''
  92. Exercise # 2:
  93. Implement the binary search using recursion without the slice operator. Recall that you
  94. will need to pass the list along with the starting and ending index values for the sublist.
  95. Generate a random, ordered list of integers and do a benchmark analysis.
  96. '''
  97. def recur_bin_srch_noslice(alist,first,last,item):
  98.     """Recursive binary search without the slice
  99.    Arguments:
  100.        {List}
  101.        {Integer} - First index in list
  102.        {Integer} - Last index in list
  103.        {Object} - Object to search for
  104.    Returns: {Boolean}
  105.    """
  106.     if last>=first:
  107.         midpoint = (first + last)//2
  108.         if alist[midpoint]==item:
  109.             return True
  110.         else:
  111.             if alist[midpoint] > item:
  112.                 return recur_bin_srch_noslice(alist,first,midpoint-1,item)
  113.             else:
  114.                 return recur_bin_srch_noslice(alist,midpoint+1,last,item)
  115.     else:
  116.         return False
  117.  
  118. def time_test_new_binary_search():
  119.     """Test recursive binary search function w/o slice vs non recursive binary search function
  120.    Must add time decorator on rec_bin_search() function and recur_bin_srch_noslice() function
  121.    Returns: {String} -- Time results for 2 algorithms
  122.    """
  123.     lst = [i for i in range(10000000)]
  124.     item = random.randint(0,10000000)
  125.     a,b = rec_bin_search(lst,item)
  126.     x,y = recur_bin_srch_noslice(lst,0,len(lst)-1,item)
  127.    
  128.     return f'Recursive Time:{b}, Recursive Time w/o Slice:{y}'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement