On Wed, 9 Mar 2005 04:27:10 +0900, Bill Guindon <agorilla / gmail.com> wrote:
> given 5 variables... incoming, minimium, current, maximum, reserve
> 
> produce a text file that shows every possible map of the variable relations.
> 
> Something along the lines of this:
> 
>   minimum < current < hidden = incoming = reserve
> 
> For cases such as the above that have two or more equal variables, the
> names should be sorted alphabetically, and duplicates removed:
> 
> keep: minimum < current < hidden = incoming = reserve
> 
> drop: minimum < current < incoming = hidden = reserve
> drop: minimum < current < incoming = reserve = hidden
> etc.
> 
> btw, if you think this is quiz worthy, don't post possible answers --
> but feel free to send "solutions" or  suggestions to me off list.
> 
> --
> Bill Guindon (aka aGorilla)
> 
> 

A little quiz is always nice. Attatched my solution, maybe not the
best readable but nice and easy.

best regards,

Brian

---8<----8<----
variables = %w(incoming minimum current maximum reserve)
values = (0...variables.length).to_a

def mapto(variables, values)
	return [[]] if variables.empty?
	result = []
	var, *variables = *variables
	values.each do | val |
		mapto(variables, values).each do | mapping |
			result << (mapping << [val, var])
		end
	end
	result
end

maps = mapto(variables, values).map{ | map |
	map.sort.inject(){ | (lv, lt), (rv, rt) | [rv, "#{lt} #{lv<rv ? '<' :
'='} #{rt}"] }[1]
}.sort.uniq

puts maps
---8<----8<----

-- 
Brian Schröäer
http://ruby.brian-schroeder.de/