In [1]:
l = [1, 4, 3];

In [2]:
l


Out[2]:
[1, 4, 3]

In [4]:
l.append(6)

In [5]:
l


Out[5]:
[1, 4, 3, 6]

In [6]:
l+l


Out[6]:
[1, 4, 3, 6, 1, 4, 3, 6]

In [7]:
3*l


Out[7]:
[1, 4, 3, 6, 1, 4, 3, 6, 1, 4, 3, 6]

In [8]:
l


Out[8]:
[1, 4, 3, 6]

In [9]:
range(10)


Out[9]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [11]:
range(3, 10, 2)


Out[11]:
[3, 5, 7, 9]

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

In [16]:
listb(4)


Out[16]:
[[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 [17]:
[len(listb(i)) for i in range(10)]


Out[17]:
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]

In [19]:
p = Primes()

In [20]:
p


Out[20]:
Set of all prime numbers: 2, 3, 5, 7, ...

In [22]:
l = [3,2,5,6]




In [23]:
it = iter(l)

In [24]:
it


Out[24]:
<listiterator object at 0x7f0c12639550>

In [25]:
it.next()


Out[25]:
3

In [26]:
it.next()


Out[26]:
2

In [27]:
it.next()


Out[27]:
5

In [28]:
it.next()


Out[28]:
6

In [29]:
it.next()


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

StopIteration: 

In [30]:
def listCompo(n):
    if n == 0:
        return [[]]
    res = []
    for k in range(1, n+1):
        res.extend([[k] + l for l in listCompo(n-k)])
    return res

In [31]:
listCompo(0)


Out[31]:
[[]]

In [32]:
listCompo(1)


Out[32]:
[[1]]

In [33]:
listCompo(2)


Out[33]:
[[1, 1], [2]]

In [34]:
listCompo(3)


Out[34]:
[[1, 1, 1], [1, 2], [2, 1], [3]]

In [35]:
listCompo(4)


Out[35]:
[[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 3], [2, 1, 1], [2, 2], [3, 1], [4]]

In [36]:
[len(listCompo(i)) for i in range(11)]


Out[36]:
[1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512]

In [38]:
def ajoute1(l):
    res = list(l) # copie l
    res[0]+=1
    return res

In [40]:
l = [4,3,1,2]; ajoute1(l), l


Out[40]:
([5, 3, 1, 2], [4, 3, 1, 2])

In [43]:
def listC(n):
    if n == 0:
        return [[]]
    if n == 1:
        return [[1]]
    rec = listC(n-1)
    return [[1] + l for l in rec] + [ajoute1(l) for l in rec]

In [46]:
listC(6)


Out[46]:
[[1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 2],
 [1, 1, 1, 2, 1],
 [1, 1, 1, 3],
 [1, 1, 2, 1, 1],
 [1, 1, 2, 2],
 [1, 1, 3, 1],
 [1, 1, 4],
 [1, 2, 1, 1, 1],
 [1, 2, 1, 2],
 [1, 2, 2, 1],
 [1, 2, 3],
 [1, 3, 1, 1],
 [1, 3, 2],
 [1, 4, 1],
 [1, 5],
 [2, 1, 1, 1, 1],
 [2, 1, 1, 2],
 [2, 1, 2, 1],
 [2, 1, 3],
 [2, 2, 1, 1],
 [2, 2, 2],
 [2, 3, 1],
 [2, 4],
 [3, 1, 1, 1],
 [3, 1, 2],
 [3, 2, 1],
 [3, 3],
 [4, 1, 1],
 [4, 2],
 [5, 1],
 [6]]

Iteration et Yield en Python


In [47]:
def demoyield():
    yield 1
    yield "toto"
    yield 7

In [48]:
it = demoyield()

In [49]:
it


Out[49]:
<generator object demoyield at 0x7f0c12763460>

In [50]:
it.next()


Out[50]:
1

In [51]:
it.next()


Out[51]:
'toto'

In [52]:
it.next()


Out[52]:
7

In [53]:
it.next()


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

StopIteration: 

In [54]:
def bla(n):
    for i in range(n):
        yield i

In [55]:
it = bla(5)

In [56]:
it.next()


Out[56]:
0

In [57]:
it.next()


Out[57]:
1

In [58]:
list(it)


Out[58]:
[2, 3, 4]

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

In [61]:
it = genBin(4)

In [62]:
it.next()


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

In [63]:
it.next()


Out[63]:
[0, 0, 0, 1]

In [64]:
list(it)


Out[64]:
[[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]]

Memoization


In [68]:
@cached_function
def binom(n, k):
    if k == 0:
        return 1
    if k == n: 
        return 1
    return binom(n-1, k) + binom(n-1, k-1)

In [69]:
binom(100, 50)


Out[69]:
100891344545564193334812497256

In [ ]:


In [ ]:


In [ ]: