Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def get_distance(*args):
- maxint = max(args)
- minint = min(args)
- return maxint - minint
- def find_shortest_bounds(*args):
- index_list = list()
- best_window = list()
- best_distance = sys.maxint
- for x in xrange(0, len(args)):
- index_list.append(0)
- while True:
- cur_window = [args[x][index_list[x]]
- for x in xrange(0, len(args))]
- distance = get_distance(*cur_window)
- if distance < best_distance:
- best_window = cur_window
- best_distance = distance
- lowest_index = cur_window.index(min(*cur_window))
- index_list[lowest_index] += 1
- if index_list[lowest_index] == len(args[lowest_index]):
- break
- return best_distance, tuple(best_window)
- def long_find_shortest_bounds(a,b,c):
- distance = sys.maxint
- cur_bounds = tuple()
- for a_word in a:
- for b_word in b:
- for c_word in c:
- cur_dist = get_distance(a_word, b_word, c_word)
- if cur_dist < distance:
- distance = cur_dist
- cur_bounds = (a_word, b_word, c_word)
- return distance, cur_bounds
- def create_ascending_array(length=10):
- ascend = list()
- cur_num = -sys.maxint + 1
- for x in xrange(0, length):
- cur_num = random.randint(cur_num, sys.maxint)
- ascend.append(cur_num)
- return ascend
- while True:
- a = create_ascending_array()
- b = create_ascending_array()
- c = create_ascending_array()
- fast_dist, fast_bounds = find_shortest_bounds(a,b,c)
- slow_dist, slow_bounds = long_find_shortest_bounds(a,b,c)
- print "Fast: %s, Slow: %s, Equal? %s" % (fast_dist, slow_dist, fast_dist==slow_dist)
- 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