Created
May 1, 2017 14:20
-
-
Save fmasanori/f19cc1ca3c5299d0786f4c54d9ade75b to your computer and use it in GitHub Desktop.
Gera todas as subsequências de uma lista
This file contains hidden or 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 enumerações(items): | |
n = len(items) | |
s = [0]*(n+1) | |
k = 0 | |
while True: | |
if s[k] < n: | |
s[k+1] = s[k] + 1 | |
k += 1 | |
else: | |
s[k-1] += 1 | |
k -= 1 | |
if k == 0: | |
break | |
else: | |
lista = [] | |
for j in range(1, k+1): | |
lista.append(items[s[j]-1]) | |
yield lista | |
def combinações(items, n): | |
if n==0: yield [] | |
else: | |
for i in range(len(items)): | |
for cc in combinações(items[:i]+items[i+1:],n-1): | |
yield [items[i]]+cc | |
def permutações(items): | |
return combinações(items, len(items)) | |
for p in enumerações([1, 2, 3, 4]): | |
print (p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment