Created
December 9, 2015 09:05
-
-
Save epitron/ee8a999caa0713108194 to your computer and use it in GitHub Desktop.
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
require 'pp' | |
class Array | |
def perms | |
return to_enum(:perms) unless block_given? | |
if size <= 1 | |
yield self | |
else | |
for i in 0...size | |
e = self[i] | |
left = self[0...i] | |
right = self[i+1..-1] | |
p i: i, e: e, self: self, left: left, right: right | |
left.perms.each do |lperm| | |
right.perms.each do |rperm| | |
yield [e, *lperm, *rperm] | |
end | |
end | |
end | |
end | |
end | |
end | |
a = [1,2,3] | |
p1 = a.perms.sort | |
p2 = a.permutation(a.size).sort | |
pp p1size: p1.size, p2size: p2.size | |
pp both: p1 & p2 | |
pp mine: p1 - p2 | |
pp ruby: p2 - p1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment