Created
October 11, 2013 08:31
-
-
Save uchan-nos/6931483 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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