Created
March 29, 2015 02:06
-
-
Save ahirschberg/dd4449447aa274b002dd to your computer and use it in GitHub Desktop.
Sam's all permutations of a list in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Sam's python implementation | |
| def permutations(listt): # apparently 'list' is a reserved keyword in python (or at least is a different color in the editor) | |
| if len(listt) == 1: | |
| yield listt | |
| return | |
| else: | |
| for i, o in enumerate(listt): | |
| listt_copy = listt.copy() | |
| del listt_copy[i] | |
| for perm in permutations(listt_copy): | |
| yield [o] + perm # still not quite clear on how the yield statements work here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment