Skip to content

Instantly share code, notes, and snippets.

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