Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. class Permute:
  2. def __init__(self, l):
  3. self.list = l
  4. def __iter__(self):
  5. if len(self.list) == 0:
  6. yield self.list
  7. elif len(self.list) == 1:
  8. yield self.list
  9. elif len(self.list) == 2:
  10. yield self.list
  11. yield self.list[::-1]
  12. else:
  13. for index, element in enumerate(self.list):
  14. singleList = [element]
  15. remainderList = self.list[:index] + self.list[index+1:]
  16.  
  17. # permute the rest of the list using recursion
  18. for j in Permute(remainderList):
  19. yield singleList + j
  20.  
  21. def main():
  22. l = ['a','b','c','d']
  23. for i in Permute(l):
  24. print(i)
  25.  
  26. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement