Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   [QUIZ] Unit Conversion (#183) (http://www.velocityreviews.com/forums/t854176-quiz-unit-conversion-183-a.html)

 Matthew Moss 11-14-2008 03:49 PM

[QUIZ] Unit Conversion (#183)

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

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.

 Trans 11-14-2008 06:55 PM

Re: Unit Conversion (#183)

On Nov 14, 10:49=A0am, Matthew Moss <m...@moss.name> wrote:
> -=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.

 Matthew Moss 11-14-2008 07:20 PM

Re: Unit Conversion (#183)

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

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

 Sebastian Hungerecker 11-14-2008 07:25 PM

Re: Unit Conversion (#183)

Matthew Moss wrote:
> 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: sepp2k@jabber.org
ICQ: 205544826

 Matthew Moss 11-14-2008 09:17 PM

Re: Unit Conversion (#183)

On Nov 14, 2008, at 1:25 PM, Sebastian Hungerecker wrote:

> Matthew Moss wrote:
>> Note to everyone else: using another lib, such as stick, is
>> considered
>> cheating for this quiz.

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

 Ryan Davis 11-15-2008 12:12 AM

Re: Unit Conversion (#183)

On Nov 14, 2008, at 11:20 , Matthew Moss wrote:

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

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

 Matthew Moss 11-15-2008 12:29 AM

Re: Unit Conversion (#183)

On Nov 14, 2008, at 6:12 PM, Ryan Davis wrote:

>
> On Nov 14, 2008, at 11:20 , Matthew Moss wrote:
>
>>>> Note to everyone else: using another lib, such as stick, is
>>>> considered cheating for this quiz. :D

>
> 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. ;)

 Todd Benson 11-15-2008 02:27 AM

Re: Unit Conversion (#183)

On Fri, Nov 14, 2008 at 6:12 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
>
> On Nov 14, 2008, at 11:20 , Matthew Moss wrote:
>
>>>> Note to everyone else: using another lib, such as stick, is considered
>>>> cheating for this quiz. :D

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

 Peter Szinek 11-15-2008 09:27 AM

Re: Unit Conversion (#183)

On 2008.11.14., at 19:55, Trans wrote:

>
>
> On Nov 14, 10:49 am, Matthew Moss <m...@moss.name> wrote:
>> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>>
>> 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

 Sebastian Hungerecker 11-15-2008 10:02 AM

Re: Unit Conversion (#183)

Peter Szinek wrote:
> 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

All times are GMT. The time now is 06:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.