William James
3/9/2005 12:32:00 PM
Bill Guindon 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.
This program produces 541 variations.
$rel_hash = { '0' => '=', '1' => '<' }
$var_hash = { '1' => 'incoming', '3' => 'minimium',
'0' =>'current', '2' =>'maximum', '4' =>'reserve' }
def generate( used, unused )
if 0==unused.size
all_relations( used )
else
unused.split(//).each {|c|
generate( used+c, unused.delete(c) )
}
end
end
def all_relations( s )
num = s.size - 1
(0 .. 2**(num)-1).each { |x|
show( s, sprintf( "%0#{num}b", x ) )
}
end
def ordered( s, relations )
relations.split(//).each_with_index{ |c,i|
return nil if '0'==c and s[i] > s[i+1]
}
return true
end
def show( s, relations )
if ordered( s, relations )
puts s.to_var_names( relations )
end
end
class String
def to_var_names( relations )
out = ''
self.split(//).each_with_index{ |c,i|
out += $var_hash[ c ]
out += $rel_hash[relations[i..i]] if i< relations.size
}
return out
end
end
generate( '', '01234' )