Skip to content

Instantly share code, notes, and snippets.

@cjmcgraw
Last active August 29, 2015 14:03
Show Gist options
  • Save cjmcgraw/0bdd6e48ebd962d0ccbd to your computer and use it in GitHub Desktop.
Save cjmcgraw/0bdd6e48ebd962d0ccbd to your computer and use it in GitHub Desktop.
File for testing an algorithm for finding all potential pairs of numbers
#! /usr/bin/python
from time import time
from collections import Counter
from random import randint
#########################################
# Test functions. Used for testing
########################################
VALUES = (1, 100000)
def timer(func):
def wrapper(data_one, data_two):
n = len(data_one)
t1 = time()
ret = func(data_one, data_two)
t2 = time()
print "it took {} seconds to run with {} values".format(t2 - t1, n)
return ret
return wrapper
def test(n):
main_data = [randint(*VALUES) for x in xrange(n)]
sec_data = [randint(*VALUES) for x in xrange(n)]
result = count_results(main_data, sec_data)
print "result is ---> {}".format(result)
############################################
# Start Algorithm block
############################################
# Comment out decorator to remove timing
# testing.
@timer
def count_results(data_one, data_two):
data_counter = generate_counters(data_two)
result = 0
for x in data_one:
value_one = x + 2
value_two = x - 2
result += data_counter[value_one] + data_counter[value_two]
return result
def generate_counters(data):
result = Counter()
for x in data:
result[x] += 1
return result
#############################################
# End Algorithm block
############################################
if __name__=="__main__":
for x in [10, 100, 1000, 5000, 10000, 100000, 1000000, 10000000]:
test(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment