[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A puzzle

Bill Guindon

3/8/2005 7:27:00 PM

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)


7 Answers

William James

3/9/2005 12:32:00 PM

0

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' )

Cs. Henk

3/9/2005 1:02:00 PM

0

On Wed, Mar 09, 2005 at 04:27:10AM +0900, 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

I've sent my solution already offlist, but now I see William James sent
one publicly, so I make it public as well. His solution gives 541
relation, I have 1305... interesting, which could be the good one?

Csaba

Pit Capitain

3/9/2005 1:39:00 PM

0

Cs. Henk schrieb:
> I've sent my solution already offlist, but now I see William James sent
> one publicly, so I make it public as well. His solution gives 541
> relation, I have 1305... interesting, which could be the good one?

I get 541, too. Your code seems to not pay attention to the following
restriction:

> 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:

Regards,
Pit


Pit Capitain

3/9/2005 2:14:00 PM

0

Pit Capitain schrieb:
> I get 541, too. Your code seems to not pay attention to the following
> restriction:
> ...

Looking at your code I see that the restriction *IS* handled there. The
check has to be repeated one more time, though, so it should be

((q=b[-1]).size-1).times

Then you get the same 541 results.

Regards,
Pit


Bill Guindon

3/9/2005 5:27:00 PM

0

On Wed, 9 Mar 2005 23:13:41 +0900, Pit Capitain <pit@capitain.de> wrote:
> Pit Capitain schrieb:
> > I get 541, too. Your code seems to not pay attention to the following
> > restriction:
> > ...
>
> Looking at your code I see that the restriction *IS* handled there. The
> check has to be repeated one more time, though, so it should be
>
> ((q=b[-1]).size-1).times
>
> Then you get the same 541 results.
>
> Regards,
> Pit

Got some great replies to this, on and off-list. Thanks to all who
took a shot at it. You all managed to solve that last part (which had
me stumped) of eliminating the redundancies.

Hope you enjoyed it, was asked on another list, and just seemed like a
fun one :).

--
Bill Guindon (aka aGorilla)


Cs. Henk

3/9/2005 8:54:00 PM

0

On Wed, Mar 09, 2005 at 11:13:41PM +0900, Pit Capitain wrote:
> Pit Capitain schrieb:
> >I get 541, too. Your code seems to not pay attention to the following
> >restriction:
> >...
>
> Looking at your code I see that the restriction *IS* handled there. The
> check has to be repeated one more time, though, so it should be
>
> ((q=b[-1]).size-1).times
>
> Then you get the same 541 results.

Yes, you are right! Thanks for the enlightenment!

Csaba


Brian Schröder

3/9/2005 10:35:00 PM

0

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öder
http://ruby.brian-sch...