[QUIZ] Unit Conversion (#183)

M

Matthew Moss

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

The three rules of Ruby Quiz 2:

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 2 by submitting ideas as often as you can!
Visit <http://splatbang.com/rubyquiz/>.

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.

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

## Unit Conversion (#183)

Google added a calculator to its search engine a while back. Enter
"convert 50 miles to kilometers", or even just "50 mi to km", and the
first "search" result will tell you that 50 miles is 80.4672
kilometers. This works for units other than length. Try "33 ml to
gal", "6 hours to minutes", and"50 stones to lbs", and you'll see that
Google's calculator knows a lot of different units and how to convert
between them all.

Your task is to write a units converter script. The input to the
script must be three arguments: the quantity, the source units, and
the destination units. The first example above would be run like this:

$ ruby convert.rb 50 miles kilometers

Or, using abbreviations:

$ ruby convert.rb 50 mi km

Support as many units and categories of units (i.e. volume, length,
weight, etc.) as you can, along with appropriate abbreviations for
each unit.
 
T

Trans

-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-

The three rules of Ruby Quiz 2:

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

2. =A0Support Ruby Quiz 2 by submitting ideas as often as you can!
Visit <http://splatbang.com/rubyquiz/>.

3. =A0Enjoy!

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

-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-

## Unit Conversion (#183)

Google added a calculator to its search engine a while back. Enter =A0
"convert 50 miles to kilometers", or even just "50 mi to km", and the =A0
first "search" result will tell you that 50 miles is 80.4672 =A0
kilometers. This works for units other than length. Try "33 ml to =A0
gal", "6 hours to minutes", and"50 stones to lbs", and you'll see that = =A0
Google's calculator knows a lot of different units and how to convert =A0
between them all.

Your task is to write a units converter script. The input to the =A0
script must be three arguments: the quantity, the source units, and =A0
the destination units. The first example above would be run like this:

=A0 =A0 =A0$ ruby convert.rb 50 miles kilometers

Or, using abbreviations:

=A0 =A0 =A0$ ruby convert.rb 50 mi km

Support as many units and categories of units (i.e. volume, length, =A0
weight, etc.) as you can, along with appropriate abbreviations for =A0
each unit.

This will be interesting. I'm not going to endeavor into myself b/c I
help maintain Stick (http://stick.rubyforge.org) which already does
this (and there's another lib out there that does it too). But it will
be interesting to see how others approach it.

Thanks,
T.
 
M

Matthew Moss

This will be interesting. I'm not going to endeavor into myself b/c I
help maintain Stick (http://stick.rubyforge.org) which already does
this (and there's another lib out there that does it too). But it will
be interesting to see how others approach it.

Note to everyone else: using another lib, such as stick, is considered
cheating for this quiz. :D
 
S

Sebastian Hungerecker

Matthew said:
Note to everyone else: using another lib, such as stick, is considered =A0
cheating for this quiz.

That's "another lib which does unit conversion" not "any other lib", right?

=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
M

Matthew Moss

That's "another lib which does unit conversion" not "any other lib",
right?


Yes.
Although, if it's wholly self-contained, that's worth some awesome
points.
 
R

Ryan Davis

so... `units` isn't a library... is this cheating?
def units(n, i, o) `units "#{n} #{i}" #{o}`[/[\d\.]+/].to_f; end => nil
units 50, :mi, :km
=> 80.4672

:D

I assume it is... but I also assume it isn't a real contender for the
intent of the quiz in the first place (which is a shame, because this
is much better reuse).
 
M

Matthew Moss

so... `units` isn't a library... is this cheating?
def units(n, i, o) `units "#{n} #{i}" #{o}`[/[\d\.]+/].to_f; end => nil
units 50, :mi, :km
=> 80.4672

:D

I assume it is... but I also assume it isn't a real contender for
the intent of the quiz in the first place (which is a shame, because
this is much better reuse).


Well, there are a number of past quizzes that would be rather boring
if reuse were always the primary goal. :D

Don't be boring. ;)
 
T

Todd Benson

so... `units` isn't a library... is this cheating?
def units(n, i, o) `units "#{n} #{i}" #{o}`[/[\d\.]+/].to_f; end => nil
units 50, :mi, :km
=> 80.4672

:D

I assume it is... but I also assume it isn't a real contender for the intent
of the quiz in the first place (which is a shame, because this is much
better reuse).

Don't forget mass can be converted to energy :)

Excellent quiz! Waiting for some good answers!

Todd
 
P

Peter Szinek

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

The three rules of Ruby Quiz 2:

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 2 by submitting ideas as often as you can!
Visit <http://splatbang.com/rubyquiz/>.

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.

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

## Unit Conversion (#183)

Google added a calculator to its search engine a while back. Enter
"convert 50 miles to kilometers", or even just "50 mi to km", and the
first "search" result will tell you that 50 miles is 80.4672
kilometers. This works for units other than length. Try "33 ml to
gal", "6 hours to minutes", and"50 stones to lbs", and you'll see
that
Google's calculator knows a lot of different units and how to convert
between them all.

Your task is to write a units converter script. The input to the
script must be three arguments: the quantity, the source units, and
the destination units. The first example above would be run like
this:

$ ruby convert.rb 50 miles kilometers

Or, using abbreviations:

$ ruby convert.rb 50 mi km

How about

ruby convert.rb 100 mile per minute per second foot per second squared

(read: 100 mile per minute per second *to* foot per second squared)

Maybe we should allow

ruby convert.rb x unit1 to unit2

or

ruby convert.rb x long_unit_name even_longer_unit_name

to avoid unnecessary complicated (or impossible) parameter parsing?


Cheers,
Peter
___
http://www.rubyrailways.com
 
S

Sebastian Hungerecker

Peter said:
ruby convert.rb x unit1 to unit2

or

ruby convert.rb x long_unit_name even_longer_unit_name

Or
ruby convert.rb x "long unit name" "even longer unit name"
i.e. just assume that ARGV[1] and ARGV[2] are the unit names and let the user
take care of unit names with spaces in them. Seems the most uncomplicated
option to me.

HTH,
Sebastian
 
P

Peter Szinek

[Note: parts of this message were removed to make it a legal post.]
Or
ruby convert.rb x "long unit name" "even longer unit name"
i.e. just assume that ARGV[1] and ARGV[2] are the unit names and let
the user
take care of unit names with spaces in them. Seems the most
uncomplicated
option to me.

D'oh! :)

Cheers,
Peter
___
http://www.rubyrailways.com
 
R

Robert Dober

Don't forget mass can be converted to energy :)
And patrol to dollars.
Not to mention distance to time. Well it seems at least, whenever I
ask how far is it away I get answers like 1 hour. ;)
Excellent quiz! Waiting for some good answers!
1+
Robert
 
K

Ken Bloom

Note to everyone else: using another lib, such as stick, is considered
cheating for this quiz. :D

I disagree, and think that if he actually has a useful lib, he should
show it off. When I wrote Quiz #95 (Code to S-Exp), it was useful to me
to see the rubynode and ParseTree solutions, even if some would have
considered that cheating. I wound up using the rubynode version for the
Ruby 1.8 version of the library I was writing.

(Or maybe if you say it's cheating, that means he's allowed to talk about
it during the spoiler period.)

--Ken
 
M

Matthew Moss

I disagree, and think that if he actually has a useful lib, he should
show it off.


It is cheating.

However, I never told him not to do it. ;)

Seriously, this is Ruby Quiz. I'm not going to call the cops on you
because you did something other than what I expected.
 
J

James Gray

I disagree, and think that if he actually has a useful lib, he should
show it off. When I wrote Quiz #95 (Code to S-Exp), it was useful to
me
to see the rubynode and ParseTree solutions, even if some would have
considered that cheating. I wound up using the rubynode version for
the
Ruby 1.8 version of the library I was writing.

(Or maybe if you say it's cheating, that means he's allowed to talk
about
it during the spoiler period.)

For what it's worth, I always invited people to submit whatever they
wanted when I ran the quiz. The more the merrier, I figure. Beside,
I was just going to summarize whatever the heck I wanted to anyway. ;)

James Edward Gray II
 
M

Matthew Moss

For what it's worth, I always invited people to submit whatever they
wanted when I ran the quiz. The more the merrier, I figure.
Beside, I was just going to summarize whatever the heck I wanted to
anyway. ;)


Agreed. I said it was cheating, but I didn't say "don't cheat." :D
 
R

Rubén Medellín

It would be much more fun to include some non static conversion
variables. Like:
ruby convert.rb 30 euros dollars
# Just making things more interesting
 
M

Matthew Moss

It would be much more fun to include some non static conversion
variables. Like:
ruby convert.rb 30 euros dollars
# Just making things more interesting

Go for it! :D
 
R

Robert Dober

Agreed. I said it was cheating, but I didn't say "don't cheat." :DSorry for nitpicking, but it is *not* cheating. It might not be what
you want and it is important to know what you consider a valid
submission or not. But cheating induces fraud, deceit or dishonesty. I
guess any solution having a proud
require 'mylib'
at the top does not have any of these negative properties.

Not that I want to make your job harder than it is already but
solutions that are submitted here might be interesting for the humble
reader even if they do not hit the spirit of the quiz on its
sweet-spot, and you might not review them. Would you tend to agree on
this?

Cheers
Robert
 
M

Matthew Moss

Sorry for nitpicking, but it is *not* cheating.

Provide a simple, better word for "going against intended guidelines"
and I'll consider it.

In reality, I think most people understand that it is being used
tongue-in-cheek. And, as I stated, "cheating" is acceptable.


It might not be what
you want and it is important to know what you consider a valid
submission or not.

Basically, anything within reason that solves the quiz problem is
valid, "cheating" or not.


Not that I want to make your job harder than it is already but
solutions that are submitted here might be interesting for the humble
reader even if they do not hit the spirit of the quiz on its
sweet-spot, and you might not review them. Would you tend to agree on
this?

How about just writing a solution that solves the quiz -- "cheating"
if you like, not "cheating" if you don't like -- and leave the spirits
to go haunt some Perl coder. I never have time to fully review all the
solutions, "cheating" or not. But I generally give everything a once-
over. Just because you do something that I've called cheating will not
automatically send your submission to the trash bin. On the contrary,
I'll probably point it out as "the right way" to do things.


If your main issue is that you think "cheat" implies dishonesty, then
understand I mean no such thing. gsub(/cheat/, "reuse") and you'll be
happier.
 

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,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top