Skip to content

Instantly share code, notes, and snippets.

@maxplomer
Last active August 29, 2015 14:04
Show Gist options
  • Save maxplomer/286abdf320ec3b67798c to your computer and use it in GitHub Desktop.
Save maxplomer/286abdf320ec3b67798c to your computer and use it in GitHub Desktop.
options choices list - generate all the possible combinations of a given number of choices and options to choose from
def binarylist(choices)
list = Array.new(2**choices)
(2**choices).times do |x|
list[x]=[]
choices.times do |y|
if (x & 2**y)!=0
list[x] << 1
else
list[x] << 0
end
end
end
return list
end
def optionschoiceslist(options,choices)
list = binarylist(choices)
current = 1
3.upto(options) do
newlist = [ ]
list.length.times do |i|
row = list[i]
if row.include?(current)
index = [ ]
row.length.times do |j|
if row[j]==current
index << j
end
end
templist = binarylist(index.length)
templist = templist.collect{|e1| e1.collect {|e2| e2 + current }}
templist.length.times do |j|
temprow = row.dup
index.length.times do |k|
temprow[index[k]] = templist[j][k]
end
newlist << temprow
end
else
newlist << row
end
end
current = current + 1
list = newlist
end
return list
end
optionschoiceslist(4,3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment