Skip to content

Instantly share code, notes, and snippets.

@Mikoangelo
Created December 25, 2010 02:20
Show Gist options
  • Save Mikoangelo/754628 to your computer and use it in GitHub Desktop.
Save Mikoangelo/754628 to your computer and use it in GitHub Desktop.
N = 5
def permutations digits
return [0, 1] if digits == 1
permutations(digits-1).map { |d| [0, d].flatten } +
permutations(digits-1).reverse.map { |d| [1, d].flatten }
end
puts permutations(N).map &:join
00000
00001
00011
00010
00110
00111
00101
00100
01100
01101
01111
01110
01010
01011
01001
01000
11000
11001
11011
11010
11110
11111
11101
11100
10100
10101
10111
10110
10010
10011
10001
10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment