Forum: Ruby mode

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bbc4b3fca1ae3161257a8636145b424d?d=identicon&s=25 Elliot Temple (Guest)
on 2006-06-05 08:05
(Received via mailing list)
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
http://www.curi.us/blog/
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-06-05 08:32
(Received via mailing list)
On 6/5/06, Elliot Temple <curi@curi.us> wrote:
>      end
>
>

li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last
1c1e3bdfe006a22214102fcd6434a012?d=identicon&s=25 Daniel Sheppard (Guest)
on 2006-06-05 08:41
(Received via mailing list)
> -----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.

class Array
  def mode
    count = Hash.new(0)
    each {|x| count[x] += 1 }
    count.sort_by{|k,v|v}.last[0]
  end
end
Bbc4b3fca1ae3161257a8636145b424d?d=identicon&s=25 Elliot Temple (Guest)
on 2006-06-05 09:24
(Received via mailing list)
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:

>
> 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[x] += 1 }
>     count.sort_by{|k,v|v}.last[0]
>   end
> end

that's a nice way :)

-- Elliot Temple
http://www.curi.us/blog/
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2006-06-06 10:23
(Received via mailing list)
li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.max.last
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-06-06 14:55
(Received via mailing list)
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?
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2006-06-06 16:08
(Received via mailing list)
> 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


cheers

Simon
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2006-06-06 16:28
(Received via mailing list)
> 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
5d8990b757a1305f8e9100e9e4e5073d?d=identicon&s=25 WATANABE Hirofumi (Guest)
on 2006-06-06 16:48
(Received via mailing list)
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}
This topic is locked and can not be replied to.