Skip to content

Instantly share code, notes, and snippets.

@uchan-nos
Created October 11, 2013 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uchan-nos/6931483 to your computer and use it in GitHub Desktop.
Save uchan-nos/6931483 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import time
def separate_variations2(lst):
n = len(lst)
assert n >= 2
for bitpattern in xrange(1, 2 ** (n - 1)):
x = list()
y = list()
pat = bitpattern
for i in lst:
if bitpattern & 1 == 1:
x.append(i)
else:
y.append(i)
bitpattern = bitpattern >> 1
yield tuple(x), tuple(y)
def separate_variations(lst):
"""
separate a list into 2 lists
"""
n = len(lst)
assert n >= 2
s = set(lst)
for i in xrange(1, 2 ** (n - 1)):
x = {lst[c] for c, b in enumerate(reversed(format(i, 'b'))) if b == '1'}
y = s - x
yield tuple(x), tuple(y)
mylst = range(0, 20)
time0 = time.clock()
for x in separate_variations(mylst):
pass
time1 = time.clock()
for x in separate_variations2(mylst):
pass
time2 = time.clock()
print time1 - time0
print time2 - time1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment