Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created June 19, 2015 19:02
Show Gist options
  • Save zaltoprofen/e1d4a49b1016aa4507c1 to your computer and use it in GitHub Desktop.
Save zaltoprofen/e1d4a49b1016aa4507c1 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from itertools import chain, tee
from functools import reduce
class powerset(object):
def __init__(self, orig):
self._s = list(orig)
self._b = [False for _ in orig]
def __iter__(self):
while True:
yield list(map(lambda x:x[0], filter(lambda x:x[1], zip(self._s, self._b))))
try:
i = self._b.index(False)
self._b[0:i+1] = [False for _ in range(i+1)]
self._b[i] = True
except ValueError:
raise StopIteration()
def power2(s):
ps = [0 for _ in s]
for x in s:
ps += list(map(lambda z:z+[x], ps))
return ps
if __name__ == '__main__':
from random import random
s = list('寝る')
ps = list(filter(lambda _:random() < 0.5, powerset(s)))
print(ps)
if len(ps) == 2**len(s):
print('寝る冪')
else:
print('寝る冪でない')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment