Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- import time
- def method1(n):
- choices = range(n + 3)
- divider_locations = itertools.combinations(choices, 3)
- result = []
- for d1, d2, d3 in divider_locations:
- a = d1
- b = d2 - d1 - 1
- c = d3 - d2 - 1
- d = n + 2 - d3
- result.append((a, b, c, d))
- return sorted(result)
- def method2(n):
- result = []
- for a in xrange(n + 1):
- for b in xrange(n + 1 - a):
- for c in xrange(n + 1 - a - b):
- d = n - a - b - c
- result.append((a, b, c, d))
- return sorted(result)
- n = 60
- start1 = time.time()
- val1 = method1(n)
- print 'Time 1', time.time() - start1
- start2 = time.time()
- val2 = method2(n)
- print 'Time 2', time.time() - start2
- assert val1 == val2
Advertisement
Add Comment
Please, Sign In to add comment