1

Print all the ways of arranging the letters in a word.

Given a word, print every possible rearrangement of the letters in the word.

word = input("Write your word:")

My attempt is below:

The factorial of len(word) provides the number of permutations.

count = 1 
i = len(word)
while i > 0:
 count *= i 
 i-=1
 factorial = count # i!
 f_minus = int(count/len(word)) # (i - 1)!
print("There are " + str(count) + " ways to arrange " + str(len(word)) \
 + " letters.")

Create a list to append rearranged words.

inside_list = [] 
for i in range(len(word)):
 inside_list.append('')

Create a List to contain each inside_list.

container_list = [] 
for i in range(factorial):
 container_list.append(inside_list)

The variable f_minus provides details about how many times each letter appears at the start of the word . Sticking with 'farm', f_minus = (4-1)! = 6. This tells us that each letter appears in the first position six times. The following for loop prints 'f' six times, followed by six 'a's and so on.

for index in range(factorial):
 print(index + 1, word[index//f_minus])

So the following for loop assigns letters to the first element of each nested list.

for index in range(factorial): 
 container_list[index][0] = word[index//f_minus]
print(container_list)

How can I save the iterations to the list of lists so that the 'f's, 'a's, 'r's and 'm's go into the first element of the nested lists rather than all 'm's? i.e. the first 6 lists have an 'f' as their first element, the next 6 lists have an 'a' as the first element and so on.

asked Mar 17, 2016 at 21:32
10
  • I suspect the issue is that your container_list consists entirely of references to the same inner_list. You need to create multiple lists if you want the values to be independent. I'm not sure I follow your whole code, so I'm not writing an answer with a concrete solution for you. But I'd suggest that recursion is a natural fit for creating permutations, rather than building a giant nested data structure in a single function. Commented Mar 17, 2016 at 22:04
  • Please read stackoverflow.com/help/mcve. I suspect that there is way too much code and text than in needed to illustrate your simple question. I suspect that Bickknight is right about just needing to make copies. Commented Mar 17, 2016 at 23:04
  • Thanks for both responses. Recursion vs iteration is interesting and looking back at my post I agree 'there is way too much code and text', I have read through the link and duly taken note. If I call container_list [ index ][ 0 ] the first element of all nested lists is being referenced instead of the first element in successive nested lists. If I write the container_list manually then the for-loop works but because each nested list is recognised as inside_list, I cannot reference an element in a nested list. Perhaps there is a way to deal with this without manually creating container_list. Commented Mar 18, 2016 at 16:20
  • Looking at your first sentence, why don't you just use itertools.permutations? Also, for reference thingy, you can do container_list.append(inside_list[::]) Commented Jul 29, 2016 at 14:43
  • @Lafexlos: I can't argue with that. I've just tried itertools.permutations() and it solves the problem before I get a chance to think about what is happening. Commented Jul 29, 2016 at 15:06

1 Answer 1

1

There is already a function called permutations in itertools library for this purpose. You can use that instead of re-inventing the wheel.

from itertools import permutations
word = input("Write your word:")
print (["".join(wd) for wd in permutations(word, len(word))])
>>> Write your word:asd
>>> ['asd', 'ads', 'sad', 'sda', 'das', 'dsa']
answered Aug 8, 2016 at 10:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.