[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: numbers of a record in an array

Paul Danese

3/27/2008 2:51:00 PM

If you install the facets gem you can use "frequency"

e.g.

>irb
irb:> require 'rubygems'
=3D> false
irb:> require 'facets'
=3D> true
irb:> x =3D %w[a b d e e rkeke ele e ee e e el d]
=3D> ["a", "b", "d", "e", "e", "rkeke", "ele", "e", "ee", "e", "e", "el",
"d"]
irb:> x.frequency
=3D> {"ee"=3D>1, "a"=3D>1, "b"=3D>1, "d"=3D>2, "e"=3D>5, "el"=3D>1, "ele=
"=3D>1,
"rkeke"=3D>1}

-----Original Message-----
From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf
Of Bu Mihai
Sent: Tuesday, March 25, 2008 5:09 AM
To: ruby-talk ML
Subject: numbers of a record in an array

a have an array array=3D["a","b","c","a","b"]
how can i find the numbers of "a", "b","c" items in the array?
--=20
Posted via http://www.ruby-....


=0AThis email and any attached files are confidential and intended solel=
y for the intended recipient(s). If you are not the named recipient you=
should not read, distribute, copy or alter this email. Any views or opi=
nions expressed in this email are those of the author and do not represe=
nt those of the company. Warning: Although precautions have been taken=
to make sure no viruses are present in this email, the company cannot=
accept responsibility for any loss or damage that arise from the use=
of this email or attachments.

5 Answers

Bu Mihai

3/28/2008 7:28:00 AM

0

Paul Danese wrote:
> If you install the facets gem you can use "frequency"
>
> e.g.
>
>>irb
> irb:> require 'rubygems'
> => false
> irb:> require 'facets'
> => true
> irb:> x = %w[a b d e e rkeke ele e ee e e el d]
> => ["a", "b", "d", "e", "e", "rkeke", "ele", "e", "ee", "e", "e", "el",
> "d"]
> irb:> x.frequency
> => {"ee"=>1, "a"=>1, "b"=>1, "d"=>2, "e"=>5, "el"=>1, "ele"=>1,
> "rkeke"=>1}

Tnx a lot for this gem, it looks very interesting not just for solving
this issue.
--
Posted via http://www.ruby-....

jzakiya

3/28/2008 3:39:00 PM

0

On Mar 27, 10:51 am, Paul Danese <pdan...@Rib-x.com> wrote:
> If you install the facets gem you can use "frequency"
>
> e.g.
>
> >irb
>
> irb:> require 'rubygems'
> => false
> irb:> require 'facets'
> => true
> irb:> x = %w[a b d e e rkeke ele e ee e e el d]
> => ["a", "b", "d", "e", "e", "rkeke", "ele", "e", "ee", "e", "e", "el",
> "d"]
> irb:> x.frequency
> => {"ee"=>1, "a"=>1, "b"=>1, "d"=>2, "e"=>5, "el"=>1, "ele"=>1,
> "rkeke"=>1}
>

I installed facets 2.4.0 (the current latest) and tried to use the
'frequency' method shown in this example, but in doesn't exist in this
version of facets.

I did 'ri facets' and it only returned 'Enumerable#frequency'
This is using Linux (PCLOS) and Ruby 1.8.6-p114.

What version of facets does the 'frequency' method exist in, because
it isn't in version 2.4.0?

jzakiya

3/28/2008 6:21:00 PM

0

On Mar 28, 11:39 am, jzakiya <jzak...@mail.com> wrote:
> On Mar 27, 10:51 am, Paul Danese <pdan...@Rib-x.com> wrote:
>
>
>
> > If you install the facets gem you can use "frequency"
>
> > e.g.
>
> > >irb
>
> > irb:> require 'rubygems'
> > => false
> > irb:> require 'facets'
> > => true
> > irb:> x = %w[a b d e e rkeke ele e ee e e el d]
> > => ["a", "b", "d", "e", "e", "rkeke", "ele", "e", "ee", "e", "e", "el",
> > "d"]
> > irb:> x.frequency
> > => {"ee"=>1, "a"=>1, "b"=>1, "d"=>2, "e"=>5, "el"=>1, "ele"=>1,
> > "rkeke"=>1}
>
> I installed facets 2.4.0 (the current latest) and tried to use the
> 'frequency' method shown in this example, but in doesn't exist in this
> version of facets.
>
> I did 'ri facets' and it only returned 'Enumerable#frequency'
> This is using Linux (PCLOS) and Ruby 1.8.6-p114.
>
> What version of facets does the 'frequency' method exist in, because
> it isn't in version 2.4.0?

Correction:
I did 'ri frequency' NOT 'ri facets'

Piyush Ranjan

3/28/2008 10:19:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

A faster(running time) way is this

def freq2(a)
a_sorted=a.sort;temp=0;h=Hash.new(0);
a_sorted.uniq.each{|x|
h[x]=a_sorted.rindex(x)+1-temp;
temp+=h[x]
}
end

I did a small test and this method seems to running 5 times faster than the
inject one!

>> start=Time.now; 1000.times{|x| freq1(a*x)}; stop=Time.now; puts
stop-start
16.413787

>> start=Time.now; 1000.times{|x| freq2(a*x)}; stop=Time.now; puts
stop-start
3.17855

This seemed counter intuitive as freq2 is doing a lot more than freq1.
Is inject that slow (or have I done something wrong)???

Piyush
On Fri, Mar 28, 2008 at 11:55 PM, jzakiya <jzakiya@mail.com> wrote:

> On Mar 28, 11:39 am, jzakiya <jzak...@mail.com> wrote:
> > On Mar 27, 10:51 am, Paul Danese <pdan...@Rib-x.com> wrote:
> >
> >
> >
> > > If you install the facets gem you can use "frequency"
> >
> > > e.g.
> >
> > > >irb
> >
> > > irb:> require 'rubygems'
> > > => false
> > > irb:> require 'facets'
> > > => true
> > > irb:> x = %w[a b d e e rkeke ele e ee e e el d]
> > > => ["a", "b", "d", "e", "e", "rkeke", "ele", "e", "ee", "e", "e",
> "el",
> > > "d"]
> > > irb:> x.frequency
> > > => {"ee"=>1, "a"=>1, "b"=>1, "d"=>2, "e"=>5, "el"=>1, "ele"=>1,
> > > "rkeke"=>1}
> >
> > I installed facets 2.4.0 (the current latest) and tried to use the
> > 'frequency' method shown in this example, but in doesn't exist in this
> > version of facets.
> >
> > I did 'ri facets' and it only returned 'Enumerable#frequency'
> > This is using Linux (PCLOS) and Ruby 1.8.6-p114.
> >
> > What version of facets does the 'frequency' method exist in, because
> > it isn't in version 2.4.0?
>
> Correction:
> I did 'ri frequency' NOT 'ri facets'
>
>

jzakiya

3/29/2008 5:01:00 AM

0

On Mar 28, 11:39 am, jzakiya <jzak...@mail.com> wrote:
> On Mar 27, 10:51 am, Paul Danese <pdan...@Rib-x.com> wrote:
>
>
>
> > If you install the facets gem you can use "frequency"
>
> > e.g.
>
> > >irb
>
> > irb:> require 'rubygems'
> > => false
> > irb:> require 'facets'
> > => true
> > irb:> x = %w[a b d e e rkeke ele e ee e e el d]
> > => ["a", "b", "d", "e", "e", "rkeke", "ele", "e", "ee", "e", "e", "el",
> > "d"]
> > irb:> x.frequency
> > => {"ee"=>1, "a"=>1, "b"=>1, "d"=>2, "e"=>5, "el"=>1, "ele"=>1,
> > "rkeke"=>1}
>
> I installed facets 2.4.0 (the current latest) and tried to use the
> 'frequency' method shown in this example, but in doesn't exist in this
> version of facets.
>
> I did 'ri facets' and it only returned 'Enumerable#frequency'
> This is using Linux (PCLOS) and Ruby 1.8.6-p114.
>
> What version of facets does the 'frequency' method exist in, because
> it isn't in version 2.4.0?

OK, I found what is going on with facets and the 'frequency' method.

In facets 2.3.0 (2008/2/18) 'frequency' is a method under Enumerable,
which got loaded by doing

require 'rubygems'
require 'facets'

The current facets 2.4.0 (2008/3/24) 'frequency' has been moved to the
'probability.rb' file under
the Enumerable module. So to load 'frequency' under facets 2.4.0 you
must do

require 'rubygems'
require 'facets/enumerable/probability.rb'

I would consider this a bug for breaking prior usage (by hiding the
method in probability.rb).

If you go to http://rubyforge.org/frs/?group_id=804&releas...
and download the tar/zip files for each version you can see the
changes.

This is the facets source for 'frequency'

def frequency
#probs = Hash.new(0)
#each do |e|
# probs[e] += 1
#end
#probs
inject(Hash.new(0)){|h,v| h[v]+=1; h}
end