In [1]:
def binlist(n):
    if n == 0: return [[]]
    res = []
    for l in binlist(n-1):
        res.append(l+[0])
        res.append(l+[1])
    return res

In [2]:
binlist(4)


Out[2]:
[[0, 0, 0, 0],
 [0, 0, 0, 1],
 [0, 0, 1, 0],
 [0, 0, 1, 1],
 [0, 1, 0, 0],
 [0, 1, 0, 1],
 [0, 1, 1, 0],
 [0, 1, 1, 1],
 [1, 0, 0, 0],
 [1, 0, 0, 1],
 [1, 0, 1, 0],
 [1, 0, 1, 1],
 [1, 1, 0, 0],
 [1, 1, 0, 1],
 [1, 1, 1, 0],
 [1, 1, 1, 1]]

In [3]:
len(binlist(4))


Out[3]:
16

In [5]:
def binlist1(n):
    if n == 0: return [[]]
    rec = binlist1(n-1)
    return [[0]+l for l in rec] +[[1]+l for l in rec]

In [6]:
binlist1(4) == binlist(4)


Out[6]:
True

In [7]:
def binfirst(n):
    return [0]*n

In [9]:
binfirst(4)


Out[9]:
[0, 0, 0, 0]

In [27]:
def binnext(w):
    w = w[:]
    i = len(w)-1
    while i>=0:
        if w[i] == 1: 
            w[i] = 0
            i -= 1
        else:
            w[i] = 1
            return w
    raise StopIteration

In [21]:
l = [1,0,1,1]; binnext(l)


Out[21]:
[1, 1, 0, 0]

In [22]:
l


Out[22]:
[1, 0, 1, 1]

In [23]:
def binlist3(n):
    try:
        c = binfirst(n)
    except StopIteration:
        return []
    res = [c]
    while True:
        try:
            c = binnext(c)
        except StopIteration:
            return res
        res.append(c)

In [24]:
binnext([0,0,0,0])


Out[24]:
[0, 0, 0, 1]

In [25]:
binlist3(4)


Out[25]:
[[0, 0, 0, 0],
 [0, 0, 0, 1],
 [0, 0, 1, 0],
 [0, 0, 1, 1],
 [0, 1, 0, 0],
 [0, 1, 0, 1],
 [0, 1, 1, 0],
 [0, 1, 1, 1],
 [1, 0, 0, 0],
 [1, 0, 0, 1],
 [1, 0, 1, 0],
 [1, 0, 1, 1],
 [1, 1, 0, 0],
 [1, 1, 0, 1],
 [1, 1, 1, 0],
 [1, 1, 1, 1]]

In [70]:
class BinIter():
    def __init__(self, n):
        self._n = n
        self._c = None
    def __iter__(self):
        return self
    def next(self):
        if self._c is None:
            self._c = binfirst(self._n)
        else:
            self._c = binnext(self._c)
        return self._c

In [67]:
it =  BinIter(2)

In [68]:
it.next()


Out[68]:
[0, 0]

In [ ]:


In [ ]:


In [ ]:


In [43]:
for b in BinIter(3):
    print b


[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]

In [69]:
it = BinIter(3)
for w in it:
    pass
for w in it:
    print w


[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]

In [60]:
b = BinIter(2)

In [61]:
b.next()


Out[61]:
[0, 0]

In [62]:
list(b)


Out[62]:
[[0, 1], [1, 0], [1, 1]]

In [ ]:


In [ ]:


In [ ]:


In [71]:
def essai():
    for i in range(5):
        yield i

In [72]:
for i in essai(): print i


0
1
2
3
4

In [73]:
it = essai()

In [74]:
it.next()


Out[74]:
0

In [75]:
it.next()


Out[75]:
1

In [79]:
it.next()


------------------------------------------------
StopIteration  Traceback (most recent call last)
<ipython-input-79-24d6ab57088b> in <module>()
----> 1 it.next()

StopIteration: 

In [ ]:


In [80]:
def bingener(n):
    c = binfirst(n)
    while True:
        yield c
        c = binnext(c)

In [81]:
list(bingener(2))


Out[81]:
[[0, 0], [0, 1], [1, 0], [1, 1]]

In [83]:
def bingener2(n):
    if n == 0: 
        yield []
    else:
        for l in bingener2(n-1):
            yield l+[0]
            yield l+[1]

In [87]:
g = bingener2(300)

In [90]:
g.next()


Out[90]:
[0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0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,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 1,
 0]

In [92]:
2**300


Out[92]:
2037035976334486086268445688409378161051468393665936250636140449354381299763336706183397376L

In [ ]:


In [95]:
def iterprod(it1, it2):
    for a in it1:
        for b in it2:
            yield((a,b))

In [107]:
it = iterprod(["a", "b", "c"], [1,2,3])

In [111]:
it.next()


Out[111]:
('b', 1)

In [112]:
list(it)


Out[112]:
[('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]

In [114]:
class ListBin(object):
    """
    Modelise l'ensemble des listes binaires de taille n
    """
    def __init__(self, n):
        self._n = n
    def count(self):
        return 2**self._n
    def __iter__(self):
        if self._n == 0: 
            yield []
        else:
            for l in ListBin(self._n-1):
                yield l+[0]
                yield l+[1]

In [117]:
lb = ListBin(5)

In [118]:
for i in ListBin(5): print i


[0, 0, 0, 0, 0]
[0, 0, 0, 0, 1]
[0, 0, 0, 1, 0]
[0, 0, 0, 1, 1]
[0, 0, 1, 0, 0]
[0, 0, 1, 0, 1]
[0, 0, 1, 1, 0]
[0, 0, 1, 1, 1]
[0, 1, 0, 0, 0]
[0, 1, 0, 0, 1]
[0, 1, 0, 1, 0]
[0, 1, 0, 1, 1]
[0, 1, 1, 0, 0]
[0, 1, 1, 0, 1]
[0, 1, 1, 1, 0]
[0, 1, 1, 1, 1]
[1, 0, 0, 0, 0]
[1, 0, 0, 0, 1]
[1, 0, 0, 1, 0]
[1, 0, 0, 1, 1]
[1, 0, 1, 0, 0]
[1, 0, 1, 0, 1]
[1, 0, 1, 1, 0]
[1, 0, 1, 1, 1]
[1, 1, 0, 0, 0]
[1, 1, 0, 0, 1]
[1, 1, 0, 1, 0]
[1, 1, 0, 1, 1]
[1, 1, 1, 0, 0]
[1, 1, 1, 0, 1]
[1, 1, 1, 1, 0]
[1, 1, 1, 1, 1]

In [ ]:


In [ ]:


In [119]:
def Bnk(n, k):
    if k == 0:
        yield [0]*n
    elif k == n:
        yield [1]*n
    else:
        for w in Bnk(n-1, k):
            yield [0]+w
        for w in Bnk(n-1, k-1):
            yield [1]+w

In [121]:
list(Bnk(5,3))


Out[121]:
[[0, 0, 1, 1, 1],
 [0, 1, 0, 1, 1],
 [0, 1, 1, 0, 1],
 [0, 1, 1, 1, 0],
 [1, 0, 0, 1, 1],
 [1, 0, 1, 0, 1],
 [1, 0, 1, 1, 0],
 [1, 1, 0, 0, 1],
 [1, 1, 0, 1, 0],
 [1, 1, 1, 0, 0]]

In [ ]: