Last active
August 29, 2015 14:22
-
-
Save LeMeteore/340fc81863a9dc3757d2 to your computer and use it in GitHub Desktop.
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 pilepoile(n, taille): | |
if taille == 1: | |
return [[n]] | |
else: | |
toutes_les_listes = [] | |
for i in range(n + 1): | |
intermediates = pilepoile(n-i, taille -1) | |
for l in intermediates: | |
l.insert(0,i) | |
toutes_les_listes.extend(intermediates) | |
#if(i == n-i+1) or (i == n-i): break | |
return toutes_les_listes | |
def integers(n): | |
# returns integers until n | |
return list(range(n)) | |
def genIntegers(n): | |
# yields integers until n | |
for i in range(n): yield i | |
yield n | |
def s(n): | |
# 5, 4, 3, 2, 1, 0 | |
for i in range(n, -1, -1): | |
print(i, n-i) | |
if(i == n-i+1) or (i == n-i): | |
break | |
#from math import factorial | |
from itertools import combinations, combinations_with_replacement | |
from itertools import permutations | |
from functools import reduce | |
def combination(items, n): | |
if n==0: yield [] | |
else: | |
for i in range(len(items)): | |
for cc in combination(items[:i]+items[i+1:],n-1): | |
yield [items[i]]+cc | |
# n: le total que l'on veut avoir | |
# taille: le nbre d'elt a addition pour avoir n | |
def pp(n, taille): | |
if taille == 1: yield list(n) | |
# if taille <= 0: yield "taille invalide" | |
# if taille > n: yield "taille invalide" | |
else: | |
c = combination(list(range(n+1)), taille) | |
for t in c: | |
if sum(t) == n: | |
yield t | |
def ppp(n, taille): | |
if taille == 1: yield list(n) | |
# if taille <= 0: yield "taille invalide" | |
# if taille > n: yield "taille invalide" | |
else: | |
#c = combinations(range(n+1), taille) | |
c = combinations_with_replacement(range(n+1), taille) | |
for t in c: | |
if sum(t) == n: | |
for p in permutations(t): | |
yield list(p) | |
def pilepoile1(n, taille): | |
if taille == 1: | |
yield [n] | |
else: | |
for i in range(n+1): | |
intermediates = pilepoile(n-i, taille-1) | |
for l in intermediates: | |
yield [i] + l | |
if(i == n-i+1) or (i == n-i): | |
break | |
def pilepoile2(n, taille): | |
if taille == 1: | |
yield [n] | |
else: | |
for i in range(n+1): | |
intermediates = pilepoile(n-i, taille-1) | |
for l in intermediates: | |
for j in permutations( [i] + l): | |
yield j | |
if(i == n-i+1) or (i == n-i): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment