How do I get the program to show all the combinations and then the number of combinations that were formed? [closed]

-4

What I already have so far:

from itertools import permutations
word = str(input())
for subset in permutations(word,len(word)):
    print(subset)
    
asked by anonymous 21.10.2017 / 21:58

2 answers

3

To know the number of combinations, simply save them all in a list and check after your size.

from itertools import permutations

word = str(input())
sequences = list(permutations(word,len(word)))

for subset in sequences:
    print(subset)

print("Número de sequências:", len(sequences))

If the list is too large and you do not want to store it all in memory, just create an auxiliary counter:

from itertools import permutations

word = str(input())
sequences = permutations(word,len(word))
total = 0

for subset in sequences:
    total += 1
    print(subset)

print("Número de sequências:", total)
    
24.10.2017 / 03:07
2

I think adding this line at the end of the code outside the loop works:

print(len(permutations(word,len(word))))

Edited:

The previous solution does not work, so I would use the same Woss solution:

from itertools import permutations
word = str(input())
count = 0
for subset in permutations(word,len(word)):
    count += 1
    print(subset)
print(count)
    
24.10.2017 / 04:40