Created
October 11, 2013 07:59
-
-
Save cloverrose/6931183 to your computer and use it in GitHub Desktop.
リストを2つに分ける全バリエーションを求める。 ありがとう @uchan_nos "整数を0からカウントアップして、1が立ってるビットの要素だけを含む集合と補集合に分ければどうだろう" https://twitter.com/uchan_nos/status/388568538174066688
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment