Created
October 19, 2021 19:35
-
-
Save bsboris/34a7cc8ef4b66dc62480c27f16a01205 to your computer and use it in GitHub Desktop.
Array permutations
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 pm(n) | |
| return [] if n == 0 | |
| return [[0]] if n == 1 | |
| [].tap do |out| | |
| pm(n-1).each do |sub| | |
| n.times do |i| | |
| out << sub.dup.insert(i-=1, n-1) | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment