How do I make the program show all the combinations and then the number of combinations that were formed?

Asked

Viewed 824 times

-4

What I’ve got so far:

from itertools import permutations
word = str(input())
for subset in permutations(word,len(word)):
    print(subset)
  • I don’t think so. You have to know which font is being used in the output, the font size and the output window size. Pq needs this?

  • I am using combinatorial analysis in lists and as in each row has a different combination, I thought it would be easier to do this than to use the factorial of the number of letters to find out how many combinations there are.

  • If they are lists, it would not be easier to count the number of their elements?

  • Can you give an example?

  • Pose your problem. It is very difficult to fix a solution that has gone bad. It is better to give you another idea

  • It’s complicated because you need to know the window size, font, number of list elements and so on. It’s much easier to count the number of items in the list instead of all this.

  • @Joãovictor, I edited my question, this is the program.

  • And sorry for the delay.

  • It seems like you already have just about what you need. It already shows the possible combinations, right? What is the question?

  • I would like the program to say at the end: 'There are x possible combinations.'

  • Friend, can you tell if the solution I gave worked?

  • I added the line you said outside the loop and after showing all possible combinations gave this error:Traceback (Most recent call last): File "python", line 5, in <module> Typeerror: Object of type 'itertools.permutations' has on Len()

Show 7 more comments

2 answers

3


To know the number of combinations, just save them all in a list and check after their 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 long and you don’t 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)
  • I liked the answer, but if permutations() returns an eternal object, it is not enough to do Len(permutations())?

  • @Joãovictor No, because the return is a generator. It is eternal, but it is impossible to infer its size without going through it completely. Converting it to a list does this implicitly.

  • The Len() method does not fully traverse it?

  • @Joãovictor does not, because it would be an implementation error. A generator can only be "run" once and its previous values are lost. If the function len travel it, we could not use it anymore after that - nor before, because the len would miscalculate the amount of elements.

  • I think I get your point. But the fact that the function permutations() returns the same result after the whole code of the colleague there, does not make my solution I gave below work?

  • @Joãovictor tried to create an example: https://ideone.com/5vvo3J. His solution presents type error saying that the return of permutations cannot be used in len. Even so you will have to convert to a list (or tuple) to be able to infer about its size.

  • Look what I mean http://www.codeskulptor.org/#user43_JzGSo08mhb_0.py

Show 2 more comments

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 solution of Woss:

from itertools import permutations
word = str(input())
count = 0
for subset in permutations(word,len(word)):
    count += 1
    print(subset)
print(count)
  • John, did you ever test this code? It gives error of type saying that the return of the function permutations cannot be used in the function len, that is, your solution does not work.

  • Doesn’t really work. :(

Browser other questions tagged

You are not signed in. Login or sign up in order to post.