Created
January 15, 2011 23:31
-
-
Save ex/781372 to your computer and use it in GitHub Desktop.
permutation
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
| def permutations(array) | |
| n = array.length | |
| return [array] if n < 2 | |
| rt = Array.new | |
| for i in 0...n | |
| ## Create a new array from [array] where the | |
| ## [i]-th term has been ignored. | |
| t = Array.new(array) | |
| t.delete_at(i) | |
| tz = permutations(t) | |
| tz.each do |tp| | |
| rt << tp.unshift(array[i]) | |
| end | |
| end | |
| return rt | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment