Mode

I bet there's a more elegant way to write this. Anyone want to show me?

def mode(li)
   best_num = nil
   best_count = -1
   li.uniq.each do |item|
     count = (li.select { |item2| item == item2}).length
     if count > best_count
       best_count = count
       best_num = item
     end
   end
   best_num
end

-- Elliot Temple

li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last

···

On 6/5/06, Elliot Temple <curi@curi.us> wrote:

I bet there's a more elegant way to write this. Anyone want to show me?

def mode(li)
   best_num = nil
   best_count = -1
   li.uniq.each do |item|
     count = (li.select { |item2| item == item2}).length
     if count > best_count
       best_count = count
       best_num = item
     end
   end
   best_num
end

-- Elliot Temple
Curiosity Blog – Elliot Temple

--
-Alder

From: Elliot Temple [mailto:curi@curi.us]

I bet there's a more elegant way to write this. Anyone want
to show me?

def mode(li)

...

end

It would probably help if you said what you were trying to achieve
rather than asking people to try and interpret code that you admit isn't
elegant.

class Array
  def mode
    count = Hash.new(0)
    each {|x| count += 1 }
    count.sort_by{|k,v|v}.last[0]
  end
end

···

-----Original Message-----

li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last

cool, thanks, collecting pairs works well.

From: Elliot Temple [mailto:curi@curi.us]

I bet there's a more elegant way to write this. Anyone want
to show me?

def mode(li)

...

end

It would probably help if you said what you were trying to achieve
rather than asking people to try and interpret code that you admit isn't
elegant.

Mode means:

(Statistics) the value that occurs most frequently in a given set of data.

class Array
  def mode
    count = Hash.new(0)
    each {|x| count += 1 }
    count.sort_by{|k,v|v}.last[0]
  end
end

that's a nice way :slight_smile:

-- Elliot Temple

···

On Jun 4, 2006, at 11:31 PM, Alder Green wrote:
On Jun 4, 2006, at 11:40 PM, Daniel Sheppard wrote:

-----Original Message-----

li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.max.last

···

On Monday 05 June 2006 16:23, Elliot Temple wrote:

On Jun 4, 2006, at 11:31 PM, Alder Green wrote:
> li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last

cool, thanks, collecting pairs works well.

On Jun 4, 2006, at 11:40 PM, Daniel Sheppard wrote:
>> -----Original Message-----
>> From: Elliot Temple [mailto:curi@curi.us]
>>
>> I bet there's a more elegant way to write this. Anyone want
>> to show me?
>>
>> def mode(li)
>
> ...
>
>> end
>
> It would probably help if you said what you were trying to achieve
> rather than asking people to try and interpret code that you admit
> isn't
> elegant.

Mode means:

(Statistics) the value that occurs most frequently in a given set of
data.

> class Array
> def mode
> count = Hash.new(0)
> each {|x| count += 1 }
> count.sort_by{|k,v|v}.last[0]
> end
> end

that's a nice way :slight_smile:

-- Elliot Temple
Curiosity Blog – Elliot Temple

--
EYE CONTACT
Q: Why do men find it so difficult to make eye contact?
A: Breasts don't have eyes.

Hi Micheal,

I could not get your example to work properly. It seemed to return the
count of the highest number in my initial list.

I modified it to

li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.invert.max.last

and it gave me the result I expected. Is it just me tho?

···

On 6/6/06, Michael Fellinger <m.fellinger@gmail.com> wrote:

li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.max.last

On Monday 05 June 2006 16:23, Elliot Temple wrote:
> On Jun 4, 2006, at 11:31 PM, Alder Green wrote:
> > li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last
>
> cool, thanks, collecting pairs works well.
>
> On Jun 4, 2006, at 11:40 PM, Daniel Sheppard wrote:
> >> -----Original Message-----
> >> From: Elliot Temple [mailto:curi@curi.us]
> >>
> >> I bet there's a more elegant way to write this. Anyone want
> >> to show me?
> >>
> >> def mode(li)
> >
> > ...
> >
> >> end
> >
> > It would probably help if you said what you were trying to achieve
> > rather than asking people to try and interpret code that you admit
> > isn't
> > elegant.
>
> Mode means:
>
> (Statistics) the value that occurs most frequently in a given set of
> data.
>
> > class Array
> > def mode
> > count = Hash.new(0)
> > each {|x| count += 1 }
> > count.sort_by{|k,v|v}.last[0]
> > end
> > end
>
> that's a nice way :slight_smile:
>
> -- Elliot Temple
> Curiosity Blog – Elliot Temple

--
EYE CONTACT
Q: Why do men find it so difficult to make eye contact?
A: Breasts don't have eyes.

another one:

li.sort_by{|x| li.select{|y| x==y}.length}.last

cheers

Simon

···

-----Original Message-----
From: Daniel N [mailto:has.sox@gmail.com]
Sent: Tuesday, June 06, 2006 2:54 PM
To: ruby-talk ML
Subject: Re: mode

Hi Micheal,

I could not get your example to work properly. It seemed to
return the
count of the highest number in my initial list.

I modified it to

li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.invert.max.last

and it gave me the result I expected. Is it just me tho?

another one:

li.sort_by{|x| li.select{|y| x==y}.length}.last

even better (if === does what you expect)

li.sort_by{|x|li.grep(x).size}.last

···

cheers

Simon

Hi,

"Kroeger, Simon (ext)" <simon.kroeger.ext@siemens.com> writes:

li.sort_by{|x|li.grep(x).size}.last

In 1.9, you can use Enumerable#max_by.

li.max_by{|x|li.grep(x).size}

···

--
eban