Created
November 2, 2009 22:11
-
-
Save fronx/224531 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
| class Integer | |
| def to_binary_array(size) | |
| result = to_s(2).split(//).map{ |i| 1 if i == '1' } | |
| result = [nil] + result while result.size < size | |
| result | |
| end | |
| end | |
| class Array | |
| def map_with_other(other) | |
| result = [] | |
| each_with_index do |item, index| | |
| result[index] = yield(item, other[index]) | |
| end | |
| result | |
| end | |
| def shuffle(min_size = 1, max_size = nil) | |
| (1..(2**size - 1)).map { |s| s.to_binary_array(size) }.map do |bin_ary| | |
| map_with_other(bin_ary) do |item, bin| | |
| bin && item | |
| end.compact | |
| end.select do |row| | |
| row.size >= min_size && (!max_size || row.size <= max_size) | |
| end | |
| end | |
| end | |
| puts [1, 2, 3].shuffle(1, 2).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment