Skip to content

Instantly share code, notes, and snippets.

@fronx
Created November 2, 2009 22:11
Show Gist options
  • Select an option

  • Save fronx/224531 to your computer and use it in GitHub Desktop.

Select an option

Save fronx/224531 to your computer and use it in GitHub Desktop.
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