Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4558 > unrolled thread

Counting how many times the same elements occurs in an array?

Started byThomas Greenwood <nedpointsman@gmail.com>
First post2011-05-15 05:01 -0500
Last post2011-05-15 15:33 -0500
Articles 8 — 6 participants

Back to article view | Back to comp.lang.ruby


Contents

  Counting how many times the same elements occurs in an array? Thomas Greenwood <nedpointsman@gmail.com> - 2011-05-15 05:01 -0500
    Re: Counting how many times the same elements occurs in an array? John Feminella <johnf@bitsbuilder.com> - 2011-05-15 06:49 -0500
    Re: Counting how many times the same elements occurs in an array? Robert Klemme <shortcutter@googlemail.com> - 2011-05-15 13:52 +0200
    Re: Counting how many times the same elements occurs in an array? Thomas Greenwood <nedpointsman@gmail.com> - 2011-05-15 08:06 -0500
      Re: Counting how many times the same elements occurs in an array? Robert Klemme <shortcutter@googlemail.com> - 2011-05-15 15:37 +0200
      Re: Counting how many times the same elements occurs in an array? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-15 15:01 -0500
        Re: Counting how many times the same elements occurs in an array? Adam Prescott <adam@aprescott.com> - 2011-05-15 15:21 -0500
        Re: Counting how many times the same elements occurs in an array? David Jacobs <developer@wit.io> - 2011-05-15 15:33 -0500

#4558 — Counting how many times the same elements occurs in an array?

FromThomas Greenwood <nedpointsman@gmail.com>
Date2011-05-15 05:01 -0500
SubjectCounting how many times the same elements occurs in an array?
Message-ID<6a81112b3af069fa30e1c843c72f8be9@ruby-forum.com>
There's probably a fairly simple way to do this.

Basically I'm reading data from an xml file, I need to figure out how
many times identical data occurs in certain attributes, so far I've got
the data into two identical arrays and had the intention of nesting
iterators - seeing if the element was equal to the second and
incrementing every time a match was found. That obviously didn't work
out the way I initially thought.

This seems to be the jist of what I want but it's obviously returning a
count on every iteraton whereas I only want the final tally.

xml_events.each{|x|
  puts "#{x} occurs #{xml_events.count(x)} times"
}

Any ideas?

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

[toc] | [next] | [standalone]


#4562

FromJohn Feminella <johnf@bitsbuilder.com>
Date2011-05-15 06:49 -0500
Message-ID<BANLkTingDYvC4VP5LvHZLXNvqJteBNMhiQ@mail.gmail.com>
In reply to#4558
You didn't mention what a particular xml_event object looks like, but
you'll probably want something like this:

xml_events.group_by(&:name).each do |name, events|
  puts "there were #{events.size} events of type #{name}"
end

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Sun, May 15, 2011 at 06:01, Thomas Greenwood <nedpointsman@gmail.com> wrote:
> There's probably a fairly simple way to do this.
>
> Basically I'm reading data from an xml file, I need to figure out how
> many times identical data occurs in certain attributes, so far I've got
> the data into two identical arrays and had the intention of nesting
> iterators - seeing if the element was equal to the second and
> incrementing every time a match was found. That obviously didn't work
> out the way I initially thought.
>
> This seems to be the jist of what I want but it's obviously returning a
> count on every iteraton whereas I only want the final tally.
>
> xml_events.each{|x|
>  puts "#{x} occurs #{xml_events.count(x)} times"
> }
>
> Any ideas?
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

[toc] | [prev] | [next] | [standalone]


#4563

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-05-15 13:52 +0200
Message-ID<939t4dF4vaU1@mid.individual.net>
In reply to#4558
On 15.05.2011 12:01, Thomas Greenwood wrote:
> There's probably a fairly simple way to do this.
>
> Basically I'm reading data from an xml file, I need to figure out how
> many times identical data occurs in certain attributes, so far I've got
> the data into two identical arrays and had the intention of nesting
> iterators - seeing if the element was equal to the second and
> incrementing every time a match was found. That obviously didn't work
> out the way I initially thought.
>
> This seems to be the jist of what I want but it's obviously returning a
> count on every iteraton whereas I only want the final tally.
>
> xml_events.each{|x|
>    puts "#{x} occurs #{xml_events.count(x)} times"
> }
>
> Any ideas?

Two possible approaches:

irb(main):002:0> a = Array.new(10) { rand(4) }
=> [3, 2, 2, 1, 3, 3, 2, 3, 3, 3]

irb(main):003:0> a.inject(Hash.new(0)) {|sums,x| sums[x] += 1; sums}
=> {3=>6, 2=>3, 1=>1}

irb(main):004:0> a.group_by {|x| x}
=> {3=>[3, 3, 3, 3, 3, 3], 2=>[2, 2, 2], 1=>[1]}
irb(main):005:0> a.group_by {|x| x}.map {|k,v| [k, v.size]}
=> [[3, 6], [2, 3], [1, 1]]

Instead of #inject you can of course also use a more traditional approach:

irb(main):012:0> counts = Hash.new 0
=> {}
irb(main):013:0> a.each {|x| counts[x] += 1}
=> [3, 2, 2, 1, 3, 3, 2, 3, 3, 3]
irb(main):014:0> counts
=> {3=>6, 2=>3, 1=>1}

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#4570

FromThomas Greenwood <nedpointsman@gmail.com>
Date2011-05-15 08:06 -0500
Message-ID<d6cf848516ea814ff3552bba56a5c4ba@ruby-forum.com>
In reply to#4558
I'm sure your solutions are better than mine, what I ended up doing;

xml_events = Array.new
temp_array = Array.new

[...]
#extract xml data and assign it to the events array.
[...]

xml_events.each{|x|
  if temp_array.include?(x) == false
  temp_array << x
  puts "#{x} occurs #{xml_events.count(x)} times"
  end
}

A kludge but it does the job.

Thanks for your help.

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

[toc] | [prev] | [next] | [standalone]


#4571

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-05-15 15:37 +0200
Message-ID<93a392Fkp8U1@mid.individual.net>
In reply to#4570
On 15.05.2011 15:06, Thomas Greenwood wrote:
> I'm sure your solutions are better than mine, what I ended up doing;
>
> xml_events = Array.new
> temp_array = Array.new
>
> [...]
> #extract xml data and assign it to the events array.
> [...]
>
> xml_events.each{|x|
>    if temp_array.include?(x) == false

This is dangerous: in Ruby false and nil are treated as boolean false. 
It's better to not compare with boolean constants but rather to use 
boolean operators and logic.  In your case you could do

    if !temp_array.include?(x)
    unless temp_array.include?(x)

>    temp_array<<  x
>    puts "#{x} occurs #{xml_events.count(x)} times"
>    end
> }
>
> A kludge but it does the job.

Your code has effort O(n*n) if I am not mistaken while the approach with 
the Hash storage of counters only has O(n).  That might not really make 
a difference in your case but from the fact that you are iterating 
xml_events over and over again (same for temp_array btw.) you might see 
that it is "ugly" in a way.

> Thanks for your help.

You're welcome.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#4576

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-15 15:01 -0500
Message-ID<93494823a74377b27080735e61527c5f@ruby-forum.com>
In reply to#4570
Thomas Greenwood wrote in post #998795:
>
> A kludge but it does the job.
>

After asking for advice on a computer programming forum, the chosen 
solution should never be a kludge.  Rather, the solution should be 
elegant and inspiring, and you should learn somethin.

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

[toc] | [prev] | [next] | [standalone]


#4577

FromAdam Prescott <adam@aprescott.com>
Date2011-05-15 15:21 -0500
Message-ID<BANLkTin7DE_C0usgLfNoc0euHV49YOr+yg@mail.gmail.com>
In reply to#4576
[Note:  parts of this message were removed to make it a legal post.]

On Sun, May 15, 2011 at 9:01 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

> >
> > A kludge but it does the job.
> >
>
> After asking for advice on a computer programming forum, the chosen
> solution should never be a kludge.  Rather, the solution should be
> elegant and inspiring, and you should learn somethin.
>

If it isn't touted as the Perfect Solution to the problem, then it's much
better that we have input from people instead of having no input because of
a very high bar. At least people submitting their ideas means we get to see
how people might approach the problem even if it's potentially mistaken. In
having almost-correct code there's a record down which others can learn
from.

[toc] | [prev] | [next] | [standalone]


#4578

FromDavid Jacobs <developer@wit.io>
Date2011-05-15 15:33 -0500
Message-ID<F6294F70823D4AE8917F1B050AA7457A@wit.io>
In reply to#4576
Agreed, I'm not a huge fan of the solution. John and Robert's are much more straightforward, reusable, and elegant.

You shouldn't have to use a variable called temp_array ... almost ever.
On Sunday, 15 May 2011 at 4:01 pm, 7stud -- wrote:
Thomas Greenwood wrote in post #998795:
> > 
> > A kludge but it does the job.
> 
> After asking for advice on a computer programming forum, the chosen 
> solution should never be a kludge. Rather, the solution should be 
> elegant and inspiring, and you should learn somethin.
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web