The Little Hash Key that Could Answer and Solution – Rubeque

I found this question simple to understand, but difficult in finding how to display the “key” for the min “value”.

def key_for_min_value(hash)
hash.key (hash.values.min_by {|a| a})
end

http://www.rubeque.com/problems/the-little-hash-key-that-could

This code finds the min value, and avoids using a sort_by loop by placing the values method within the hash.key. This allows the key to be printed, while allowing values.min_by to target the smallest value.

http://zetcode.com/lang/rubytutorial/hashes/ – Helpful in learning the very basics of hashes.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s