So I'm working on a problem in which I'm to create a Python Class to generate all Permutations of a list and I'm running across the following questions:
- I can complete this easily with a simple recursive function, but as a class it seems like I would want to use the iter method. My method calls a recursive function (list_all) that's almost identical to my iter, which is very unsettling. How do I modify my recursive function to be in compliance with best practices for iter?
- I wrote this code, saw that it worked, and I feel like I don't understand it! I try to trace the code line by line in a test case, but to me it looks like the first element in the list is frozen each time, and the rest of the list is shuffled. Instead the output comes out in an unexpected order. I'm not understanding something!
Thanks!
class permutations():
def __init__(self, ls):
self.list = ls
def __iter__(self):
ls = self.list
length = len(ls)
if length <= 1:
yield ls
else:
for p in self.list_all(ls[1:]):
for x in range(length):
yield p[:x] + ls[0:1] + p[x:]
def list_all(self, ls):
length = len(ls)
if length <= 1:
yield ls
else:
for p in self.list_all(ls[1:]):
for x in range(length):
yield p[:x] + ls[0:1] + p[x:]
yieldin your function.