Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Permute:
- def __init__(self, l):
- self.list = l
- def __iter__(self):
- if len(self.list) == 0:
- yield self.list
- elif len(self.list) == 1:
- yield self.list
- elif len(self.list) == 2:
- yield self.list
- yield self.list[::-1]
- else:
- for index, element in enumerate(self.list):
- singleList = [element]
- remainderList = self.list[:index] + self.list[index+1:]
- # permute the rest of the list using recursion
- for j in Permute(remainderList):
- yield singleList + j
- def main():
- l = ['a','b','c','d']
- for i in Permute(l):
- print(i)
- main()
Advertisement
Add Comment
Please, Sign In to add comment