giovedì 5 dicembre 2019

Produzione di tutti i modi in cui posso riporre in due borse tre oggetti

def power_set (it):
    for i in range(2**len(it)):
        yield [it[j] for j in range (len(it)) if (i>>j) & 1]
def yieldAllCombos(items):
    """ use power_set to generate its set of all two bags, then for each bag
        take the remaining items &split them into a set of all two remainder bags
        for each set of two remainder bags return the first + the two bags
        """
    for bag1 in power_set(items):
        remain =[i for i in items if i not in bag1]
        for bag2 in power_set(remain):
            yield bag1, bag2
ls=['a','b','c']
ps=power_set(ls)
for p in ps:
   print(p)
ps=yieldAllCombos(ls)
for p in ps:
    print(p)

Nessun commento:

Posta un commento