Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import sys
  2.  
  3. def get_distance(*args):
  4.   maxint = max(args)
  5.   minint = min(args)
  6.   return maxint - minint
  7.  
  8.  
  9. def find_shortest_bounds(*args):
  10.   index_list = list()
  11.   best_window = list()
  12.   best_distance = sys.maxint
  13.  
  14.   for x in xrange(0, len(args)):
  15.     index_list.append(0)
  16.   while True:
  17.     cur_window = [args[x][index_list[x]]
  18.                   for x in xrange(0, len(args))]
  19.     distance = get_distance(*cur_window)
  20.     if distance < best_distance:
  21.       best_window = cur_window
  22.       best_distance = distance
  23.     lowest_index = cur_window.index(min(*cur_window))
  24.     index_list[lowest_index] += 1
  25.     if index_list[lowest_index] == len(args[lowest_index]):
  26.       break
  27.   return best_distance, tuple(best_window)
  28.  
  29.  
  30. def long_find_shortest_bounds(a,b,c):
  31.   distance = sys.maxint
  32.   cur_bounds = tuple()
  33.   for a_word in a:
  34.     for b_word in b:
  35.       for c_word in c:
  36.         cur_dist = get_distance(a_word, b_word, c_word)
  37.         if cur_dist < distance:
  38.           distance = cur_dist
  39.           cur_bounds = (a_word, b_word, c_word)
  40.   return distance, cur_bounds
  41.  
  42.  
  43. def create_ascending_array(length=10):
  44.   ascend = list()
  45.   cur_num = -sys.maxint + 1
  46.   for x in xrange(0, length):
  47.     cur_num = random.randint(cur_num, sys.maxint)
  48.     ascend.append(cur_num)
  49.   return ascend
  50.  
  51.  
  52. while True:
  53.   a = create_ascending_array()
  54.   b = create_ascending_array()
  55.   c = create_ascending_array()
  56.   fast_dist, fast_bounds = find_shortest_bounds(a,b,c)
  57.   slow_dist, slow_bounds = long_find_shortest_bounds(a,b,c)
  58.   print "Fast: %s, Slow: %s, Equal? %s" % (fast_dist, slow_dist, fast_dist==slow_dist)
  59.   print "BFast: %s, BSlow: %s, Equal? %s" % (fast_bounds, slow_bounds, fast_bounds==slow_bounds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement