[QUIZ] Counting Toothpicks (#111)

R

Ruby Quiz

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.rubyquiz.com/

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.
 
D

David Chelimsky

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.rubyquiz.com/

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?
 
J

James Edward Gray II

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
 
J

James Edward Gray II

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
 
D

David Chelimsky

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
 
D

Daniel Lucraft

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
 
B

Ben Bleything

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
 
A

alexander

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.de/test.rgb if that´s even needed :)

thanks a lot,

alexander
 
J

James Edward Gray II

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
 
C

CHubas

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.rubyquiz.com/

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.

Is there any specific rule to do group expressions? I mean, something
like
(( III x III ) + IIIII ) X III
is valid?

Thanks for the quiz.
 
J

James Edward Gray II

Is there any specific rule to do group expressions? I mean, something
like
(( III x III ) + IIIII ) X III
is valid?

No grouping. And I think I forgot to mention it in the quiz but
multiplication has precedence over addition.

James Edward Gray II
 
T

Tim Pease

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.rubyquiz.com/

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.

Is there any specific rule to do group expressions? I mean, something
like
(( III x III ) + IIIII ) X III
is valid?

Thanks for the quiz.


Ah you spoiled my idea I will try yo have a solution allowing for
parenthesis, imagine a toothpick bent like < and >, so I would assume
<II+III>x<I+IIII> (which is stupid of course) being 20 toothpicks.
Actually I feel that it will be easier to do a solution allowing for
parenthesis, but we will see ;)

Last I checked it was very hard to bend toothpicks without breaking
them. So your <parenthesis> would require two toothpicks apiece.

<ll+lll>x<l+llll> = 25 (24 toothpicks)

I think the same applies to the exponentiation operator ^ used in an
earlier post.

However, ou could Ruby syntax that one

lllllxxll = 25 (11 toothpicks)
lllll^ll = 25 (9 toothpicks)

But that doesn't really save any toothpicks at all.

James, can we omit toothpicks to represent zeros ;-)
l _ _ = 100 (1 toothpick)


Blessings,
TwP
 
D

David Chelimsky

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.rubyquiz.com/

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.

Is there any specific rule to do group expressions? I mean, something
like
(( III x III ) + IIIII ) X III
is valid?

Thanks for the quiz.


Ah you spoiled my idea I will try yo have a solution allowing for
parenthesis, imagine a toothpick bent like < and >, so I would assume
<II+III>x<I+IIII> (which is stupid of course) being 20 toothpicks.
Actually I feel that it will be easier to do a solution allowing for
parenthesis, but we will see ;)

Last I checked it was very hard to bend toothpicks without breaking
them. So your <parenthesis> would require two toothpicks apiece.

<ll+lll>x<l+llll> = 25 (24 toothpicks)

I think the same applies to the exponentiation operator ^ used in an
earlier post.

However, ou could Ruby syntax that one

lllllxxll = 25 (11 toothpicks)
lllll^ll = 25 (9 toothpicks)

But that doesn't really save any toothpicks at all.

James, can we omit toothpicks to represent zeros ;-)
l _ _ = 100 (1 toothpick)

Wouldn't the underscores be toothpicks turned sideways? I that *were*
allowed, it's certainly no less than 3 toothpicks.

Who would have thought that there would be so much depth to toothpick
philosophy?
 
J

James Edward Gray II

James, can we omit toothpicks to represent zeros ;-)
l _ _ = 100 (1 toothpick)

Zero toothpicks is represented by printing a "Hug a Tree" message. ;)

James Edward Gray II
 
L

Lincoln Anderson

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Zero toothpicks is represented by printing a "Hug a Tree" message. ;)

James Edward Gray II
I missed the answer to the post asking if we should implement roman
numerals. Which is "more" acceptable,

X x X = 100 (6)

or

|||||||||| x |||||||||| = 100 (22)

or does this post mean that this trumps all:

| "Hug a tree" "Hug a tree" = 100 (1)

Lincoln Anderson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFuqYqR8wmeqHdtdcRApWgAKCmAwZbjvFa9Y43/oNiQ9d8VQXMNgCeIsfJ
wyKwsSL6Rn/ASxxJq5l7rwg=
=Y0KH
-----END PGP SIGNATURE-----
 
A

Andrey Falko

Hi everyone,

I think I got a solution but I am not sure...can I post answers for
toothpick expressions for numbers 10 to 35 so that I can check whether
I get minimum toothpick counts?

Best regards,

Andrey Falko
 
J

James Edward Gray II

I think I got a solution but I am not sure...can I post answers for
toothpick expressions for numbers 10 to 35 so that I can check whether
I get minimum toothpick counts?

You bet. Post away!

James Edward Gray II
 
A

Andrey Falko

10: ||x|||||
11: |||||||||||
12: |||x||||
13: |||||||||||||
14: |||||||x||
15: |||x|||||
16: ||||x||||
17: ||||x||||+|
18: ||||||x|||
19: ||||||x|||+|
20: |||||x||||
21: |||||||x|||
22: |||||||||||x||
23: |||||||x|||+||
24: ||||||x||||
25: |||||x|||||
26: |||||||||||||x||
27: |||x|||x|||
28: ||||x|||||||
29: ||||x|||||||+|
30: |||||x||||||
31: |||||x||||||+|
32: ||||x||x||||
33: |||||||||||x|||
34: ||||x||||+|x||
35: |||||x|||||||
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top