0

This is the code that i have a problem with.

def permute(word):
    letters = list(word)
    print(type(letters))
    for letter in letters:
        letter_copy = letters.remove(letter)
        rtrn_list = letter + permute(letter_copy)
    return rtrn_list

w = 'ABC'
print(permute(w))

i am new to programming. someone please say where the problem is. Thanks in advance

dinskid
  • 3
  • 3
  • 3
    Possible duplicate of [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – ForceBru Jul 07 '18 at 17:40
  • i actually dont understand the error it throws – dinskid Jul 07 '18 at 17:42
  • 2
    @dinskid, it would be really interesting if you update the question with the error you are currently having. – Netwave Jul 07 '18 at 17:42
  • 1
    Hi! Welcome to SO :) You should be more specific in your questions, providing more details about your issue and what you are trying to achieve. – PieCot Jul 07 '18 at 17:43
  • @dinskid, what does `letters.remove` return? Does it return a string as you expect? – ForceBru Jul 07 '18 at 17:45
  • @dinskid letters.remove(letter) will return None, which is the reason that your program throwing "TypeError: 'NoneType' object is not iterable". Try print(list.remove.__doc__) – brainLoop Jul 07 '18 at 18:09
  • Thank you everyone. I didn't know to ask properly, I am unfamiliar with the norms. The comments helped me greatly. – dinskid Jul 07 '18 at 18:30

2 Answers2

0

Find your problem my comparing to this implementation.

def permute(string):
        '''
        Recursively finds all possible combinations of the
        elements -- or permuations -- of an input string and
        returns them as a list.
        >>>permute('abc')
        ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
        '''
        output = []
        if len(string) == 1:
            output = [string]
        else:
            for i, let in enumerate(string):
                for perm in permute(string[:i] + string[i + 1:]):
                    #print('Let is', let)
                    #print('Perm is', perm)
                    output += [let + perm]
        return output

permute('abc')
Out[ ]:
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
reka18
  • 7,440
  • 5
  • 16
  • 37
  • Thank you. Can you elaborate more on the let in enumerate part?? – dinskid Jul 07 '18 at 18:31
  • @dinskid it’s just a variable name meaning letter. I could have written it as for i,j in enumerate() – reka18 Jul 07 '18 at 18:32
  • @dinskid furthermore, enumerate is a powerful tool as it lets you assign an index, in this case i, to each letter, in this case let, in your input. – reka18 Jul 07 '18 at 18:41
0

For permutations you can use python builtin from itertools:

from itertools import permutations

p = []
for t in permutations('abc'):
    p.append(''.join(t))
print(p)

Output is:

['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Thank you Kesely. However I wanted to implement it myself. – dinskid Jul 07 '18 at 18:31
  • In manual pages you have sample implementation of `permutation()`. You can get inspiration there: https://docs.python.org/3.6/library/itertools.html#itertools.permutations – Andrej Kesely Jul 07 '18 at 18:34
  • Itertools is a great module, but you won’t learn anything from using it. It’s a convenience package for more experienced programmers who already know how to manually code. @dinskid you have the right idea if you wanna learn. – reka18 Jul 07 '18 at 18:35