Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Key Associated w/ Maximum Value in Hash

Reply
Thread Tools

Key Associated w/ Maximum Value in Hash

 
 
Timothy Baron
Guest
Posts: n/a
 
      09-09-2010
Simple question: what's the cleanest way to retrieve a key associated
with the maximum value of a hash. For instance, how do I retrieve 'b'
from the following example:

hash = Hash.new
hash['a']=4
hash['b']=10
hash['c']=7
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Josh Cheek
Guest
Posts: n/a
 
      09-09-2010
[Note: parts of this message were removed to make it a legal post.]

On Wed, Sep 8, 2010 at 9:57 PM, Timothy Baron <(E-Mail Removed)>wrote:

> Simple question: what's the cleanest way to retrieve a key associated
> with the maximum value of a hash. For instance, how do I retrieve 'b'
> from the following example:
>
> hash = Hash.new
> hash['a']=4
> hash['b']=10
> hash['c']=7
> --
> Posted via http://www.ruby-forum.com/.
>
>

How about Enumerable#max

hash = {'a'=>4,'b'=>10,'c'=>7}
max_record = hash.max { |a,b| a.last <=> b.last } # => ["b", 10]
max_index = max_record && max_record.first # => "b"

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-09-2010
On 09.09.2010 05:23, Josh Cheek wrote:

> On Wed, Sep 8, 2010 at 9:57 PM, Timothy Baron<(E-Mail Removed)>wrote:
>
>> Simple question: what's the cleanest way to retrieve a key associated
>> with the maximum value of a hash. For instance, how do I retrieve 'b'
>> from the following example:
>>
>> hash = Hash.new
>> hash['a']=4
>> hash['b']=10
>> hash['c']=7
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>

> How about Enumerable#max
>
> hash = {'a'=>4,'b'=>10,'c'=>7}
> max_record = hash.max { |a,b| a.last<=> b.last } # => ["b", 10]
> max_index = max_record&& max_record.first # => "b"


In 1.9.x you can even spare the #last:

irb(main):001:0> hash = {'a'=>4,'b'=>10,'c'=>7}
=> {"a"=>4, "b"=>10, "c"=>7}
irb(main):002:0> hash.max {|(k1,v1),(k2,v2)| v1 <=> v2}
=> ["b", 10]

Here's a version with #inject

irb(main):004:0> hash.inject([nil,0]) {|(maxk,maxv),(k,v)| v > maxv ?
[k,v] : [maxk,maxv]}.last
=> 10

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
Harry Kakueki
Guest
Posts: n/a
 
      09-09-2010
On Thu, Sep 9, 2010 at 11:57 AM, Timothy Baron <(E-Mail Removed)> wr=
ote:
> Simple question: =A0what's the cleanest way to retrieve a key associated
> with the maximum value of a hash. =A0For instance, how do I retrieve 'b'
> from the following example:
>
> hash =3D Hash.new
> hash['a']=3D4
> hash['b']=3D10
> hash['c']=3D7
> --


What if you have more than one max?

h =3D {'a'=3D>4,'b'=3D>10,'c'=3D>7,'d' =3D> 10}

p h.select{|x,i| i=3D=3Dh.values.max}.keys #> ["b", "d"]


Harry

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      09-09-2010
On Thu, Sep 9, 2010 at 2:25 AM, Robert Klemme
<(E-Mail Removed)> wrote:
> Here's a version with #inject
>
> irb(main):004:0> hash.inject([nil,0]) {|(maxk,maxv),(k,v)| v > maxv ? [k,v]
> : [maxk,maxv]}.last
> => 10


But that fails if there are only negative values in the hash.

hash = {:a => -2, :b => -4}
hash.inject([nil,0]) {|(maxk,maxv),(k,v)| v > maxv ? [k,v] :
[maxk,maxv]}.last # => 0

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      09-09-2010
hash.sort_by { |k,v| v }[-1][0]
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Adam Prescott
Guest
Posts: n/a
 
      09-09-2010
This won't capture multiple maximum values, but:

hash.invert[hash.values.max]

I'd be interested to see a benchmark profile of the suggestions.

On Thu, Sep 9, 2010 at 4:37 PM, Brian Candler <(E-Mail Removed)> wrote:
> hash.sort_by { |k,v| v }[-1][0]
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Timothy Baron
Guest
Posts: n/a
 
      09-10-2010
I've tried several of these approaches, and they all work well. I'm
curious, too, about which is the fastest, but for my small hashs,
there's no noticable difference.

Adam Prescott wrote:
> This won't capture multiple maximum values, but:
>
> hash.invert[hash.values.max]
>
> I'd be interested to see a benchmark profile of the suggestions.


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ben Giddings
Guest
Posts: n/a
 
      09-10-2010
On Sep 9, 2010, at 23:00, Timothy Baron wrote:
> I've tried several of these approaches, and they all work well. I'm
> curious, too, about which is the fastest, but for my small hashs,
> there's no noticable difference.


Don't bother optimizing it for speed unless you are sure it's a
bottleneck. Choose whichever option is the most readable and
maintainable.

Ben



 
Reply With Quote
 
Douglas Seifert
Guest
Posts: n/a
 
      09-12-2010
[Note: parts of this message were removed to make it a legal post.]

For fun:

http://www.pastie.org/1153968

$ ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
$ ruby max_hash_value.rb
{0=>-18, 1=>-5, 2=>-31, 3=>44, 4=>-13, 5=>-1, 6=>23, 7=>5, 8=>22, 9=>37}
user system total real
max 0.110000 0.000000 0.110000 ( 0.270811)
max 1.9.X 0.090000 0.000000 0.090000 ( 0.242181)
select 0.430000 0.000000 0.430000 ( 1.270329)
select (cache max) 0.080000 0.000000 0.080000 ( 0.147179)
sort_by 0.160000 0.000000 0.160000 ( 0.438671)
invert 0.080000 0.000000 0.080000 ( 0.279259)
invert (cache max) 0.060000 0.000000 0.060000 ( 0.155440)


On Fri, Sep 10, 2010 at 5:20 AM, Ben Giddings <(E-Mail Removed)>wrote:

> On Sep 9, 2010, at 23:00, Timothy Baron wrote:
>
>> I've tried several of these approaches, and they all work well. I'm
>> curious, too, about which is the fastest, but for my small hashs,
>> there's no noticable difference.
>>

>
> Don't bother optimizing it for speed unless you are sure it's a bottleneck.
> Choose whichever option is the most readable and maintainable.
>
> Ben
>
>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
Maximum length of hash key Robert Manea Perl Misc 14 12-09-2004 02:02 PM
Searching an example for a defined hash value of a nonexisting hash key Ralf Baerwaldt Perl Misc 1 07-20-2004 03:05 PM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments