[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[QUIZ] Counting Toothpicks (#111

James Gray

1/26/2007 1:47:00 PM

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rub...

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

This quiz was adapted from an ACM programming challenge at the suggestion of
Gavin Kistner.

Simple math can be done with toothpicks alone. Positive integers are just a
count of toothpicks so three becomes |||. We can use two toothpicks together to
build a plus sign (+) or even tilt those slightly to get a multiplication
operator (x). Putting all of that together, the expression ||x||||+| is another
way to express the number nine.

This weeks quiz is to write a program that takes a single command-line argument
which will be a positive integer. Your code should build a toothpick expression
to calculate the number using as few toothpicks as possible. For example:

$ruby toothpick.rb 9
|||x||| = 9 (8 toothpicks)

Don't forget to count those operators!

Posting toothpick expressions and/or counts for a given number is not spoiler
material.

48 Answers

David Chelimsky

1/26/2007 2:46:00 PM

0

On 1/26/07, Ruby Quiz <james@grayproductions.net> wrote:
> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this quiz until
> 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rub...
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
> on Ruby Talk follow the discussion. Please reply to the original quiz message,
> if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
> This quiz was adapted from an ACM programming challenge at the suggestion of
> Gavin Kistner.
>
> Simple math can be done with toothpicks alone. Positive integers are just a
> count of toothpicks so three becomes |||. We can use two toothpicks together to
> build a plus sign (+) or even tilt those slightly to get a multiplication
> operator (x). Putting all of that together, the expression ||x||||+| is another
> way to express the number nine.
>
> This weeks quiz is to write a program that takes a single command-line argument
> which will be a positive integer. Your code should build a toothpick expression
> to calculate the number using as few toothpicks as possible. For example:
>
> $ruby toothpick.rb 9
> |||x||| = 9 (8 toothpicks)
>
> Don't forget to count those operators!
>
> Posting toothpick expressions and/or counts for a given number is not spoiler
> material.

What if there are more than one solution to a given number? For
example, |||||||| and ||X|||| both use 8 toothpicks to evaluate to 8.
Is one preferred?

>
>

Krishna Dole

1/26/2007 3:23:00 PM

0

Are minus signs allowed?

||||X||||-| = 15 (12 toothpicks)


Krishna

On 1/26/07, Ruby Quiz <james@grayproductions.net> wrote:
> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this quiz until
> 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rub...
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
> on Ruby Talk follow the discussion. Please reply to the original quiz message,
> if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
> This quiz was adapted from an ACM programming challenge at the suggestion of
> Gavin Kistner.
>
> Simple math can be done with toothpicks alone. Positive integers are just a
> count of toothpicks so three becomes |||. We can use two toothpicks together to
> build a plus sign (+) or even tilt those slightly to get a multiplication
> operator (x). Putting all of that together, the expression ||x||||+| is another
> way to express the number nine.
>
> This weeks quiz is to write a program that takes a single command-line argument
> which will be a positive integer. Your code should build a toothpick expression
> to calculate the number using as few toothpicks as possible. For example:
>
> $ruby toothpick.rb 9
> |||x||| = 9 (8 toothpicks)
>
> Don't forget to count those operators!
>
> Posting toothpick expressions and/or counts for a given number is not spoiler
> material.
>
>

James Gray

1/26/2007 3:44:00 PM

0

On Jan 26, 2007, at 9:22 AM, Krishna Dole wrote:

> Are minus signs allowed?
>
> ||||X||||-| = 15 (12 toothpicks)

Let's stick with just addition and multiplication to keep things
simple. Feel free to as an option to your program though.

James Edward Gray II


James Gray

1/26/2007 3:47:00 PM

0

On Jan 26, 2007, at 8:46 AM, David Chelimsky wrote:

> What if there are more than one solution to a given number? For
> example, |||||||| and ||X|||| both use 8 toothpicks to evaluate to 8.
> Is one preferred?

I would probably go with the simpler solution (less math), but either
answer is fine.

James Edward Gray II

David Chelimsky

1/26/2007 3:48:00 PM

0

On 1/26/07, James Edward Gray II <james@grayproductions.net> wrote:
> On Jan 26, 2007, at 9:22 AM, Krishna Dole wrote:
>
> > Are minus signs allowed?
> >
> > ||||X||||-| = 15 (12 toothpicks)
>
> Let's stick with just addition and multiplication to keep things
> simple. Feel free to as an option to your program though.

Aw, man!!! I was already heading towards |||^|| = 9 (7 toothpicks).
Time for an aspirin, and then regroup.

Cheers,
David

>
> James Edward Gray II
>
>
>

Daniel Lucraft

1/26/2007 6:49:00 PM

0


> Aw, man!!! I was already heading towards |||^|| = 9 (7 toothpicks).
> Time for an aspirin, and then regroup.
>
Do we want optimal solutions, or is it just get as low as you can?

Dan

--
Posted via http://www.ruby-....

Ben Bleything

1/26/2007 7:27:00 PM

0

On Sat, Jan 27, 2007, James Edward Gray II wrote:
> On Jan 26, 2007, at 9:22 AM, Krishna Dole wrote:
>
> >Are minus signs allowed?
> >
> > ||||X||||-| = 15 (12 toothpicks)
>
> Let's stick with just addition and multiplication to keep things
> simple. Feel free to as an option to your program though.

Another interesting corollary would be to allow any roman numerals that
can be represented with toothpicks...

20 could be represented with 6: X+X, XxII, etc.

*shrug*

Ben

alexander

1/26/2007 7:48:00 PM

0

hi there,
i´m having a small problem with base64 decoding a string.
i´m porting a php script over to ruby and the decoding gives me
different results in ruby and in php. the problem is that the php
results works for the processing i do afterwards while the ruby version
doesn´t.
here´s the scripts in question:

php:
<?

$bytes = file_get_contents("test.rgb");
$bitmap = base64_decode($bytes);

$header = "";
$header .= "\xFF\xFE";
$header .= pack("n2",120,97);
$header .= "\x01";
$header .= "\xFF\xFF\xFF\xFF";

$header .= $bitmap;

file_put_contents("test_php.gd",$header);
?>

ruby:
require 'rubygems'
require 'fileutils'
require 'base64'

all_bytes = Base64.decode64(IO.read("test.rgb"))

bitmap = "\xFF\xFE"
bitmap << [120,97].pack("n2")
bitmap << "\x01"
bitmap << "\xFF\xFF\xFF\xFF"
bitmap << all_bytes

File.new("test_ruby.gd","w").puts(bitmap)

the ruby version is one byte shorter.

i´m probably missing something rather obvious here, but any pointers to
how i can make the ruby output be like the php output would be greatly
appreciated :)

i´ve uploaded the test.rgb file i´m using to here:

http://rss.fork.d... if that´s even needed :)

thanks a lot,

alexander

James Gray

1/26/2007 7:49:00 PM

0

On Jan 26, 2007, at 1:26 PM, Ben Bleything wrote:

> On Sat, Jan 27, 2007, James Edward Gray II wrote:
>> On Jan 26, 2007, at 9:22 AM, Krishna Dole wrote:
>>
>>> Are minus signs allowed?
>>>
>>> ||||X||||-| = 15 (12 toothpicks)
>>
>> Let's stick with just addition and multiplication to keep things
>> simple. Feel free to as an option to your program though.
>
> Another interesting corollary would be to allow any roman numerals
> that
> can be represented with toothpicks...
>
> 20 could be represented with 6: X+X, XxII, etc.

You people scare me. ;)

James Edward Gray II

James Gray

1/26/2007 7:50:00 PM

0

On Jan 26, 2007, at 12:49 PM, Daniel Lucraft wrote:

>
>> Aw, man!!! I was already heading towards |||^|| = 9 (7 toothpicks).
>> Time for an aspirin, and then regroup.
>>
> Do we want optimal solutions, or is it just get as low as you can?

In this problem, we optimize to save wood.

James Edward Gray II