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