#programma che produce tutti i sottoinsiemi di un insieme dato in input
def Powerset(myset):
subsets=[]
noComb=2**len(myset) #numero di combinazioni di 2 oggetti su n posti 2^n
#https://it.wikipedia.org/wiki/Insieme_delle_parti
for ci in range (0,noComb):
subset=[]
for ei in range (0,len(myset)):
if ci & 1 << ei: #operazione di and sui bit -spostamento a sinistra dei bit
subset.append(myset[ei])
subsets.append(subset)
return subsets
print (Powerset(['a','b','c']) )
Nessun commento:
Posta un commento