From: Daniel N Date: 2006-06-06T21:53:31+09:00 Subject: Re: mode ------=_Part_130_26958379.1149598406069 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 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[x] += 1 } > > > count.sort_by{|k,v|v}.last[0] > > > end > > > end > > > > that's a nice way :) > > > > -- Elliot Temple > > http://www.curi.us/blog/ > > -- > EYE CONTACT > Q: Why do men find it so difficult to make eye contact? > A: Breasts don't have eyes. > > ------=_Part_130_26958379.1149598406069--