I've stalled out on making this simpler, and I'm
seeking help pairing this beast down...
Given unused is a hash of arrays where each array has
repeated elements, make elements unique in each array and
remove all names that are not repeated MagicNumber times.
Finally, remove any hash pairs that hold an empty array.
unused.each do |source,routines|
orphans = routines.uniq
orphans.delete_if do |orphan|
repeated = routines.find_all{ |routine| routine == orphan }.size
repeated != MagicNumber
end
unused[source] = orphans
end
unused.delete_if{ |h,k| k.empty? }
unused = unused.inject(Hash.new) |result, (source, routines)|
orphans = routines.uniq.reject do |o|
routines.inject(0) { |c, r| c + (r == o ? 1 : 0) } != MagicNumber
end
orphans.empty? ? result : result.merge(source => orphans)
end
Hmm, I'm not really sure I did much better there.
James Edward Gray II
···
On Jan 12, 2008, at 9:24 AM, Bil Kleb wrote:
Given unused is a hash of arrays where each array has
repeated elements, make elements unique in each array and
remove all names that are not repeated MagicNumber times.
Finally, remove any hash pairs that hold an empty array.
unused.each do |source,routines|
orphans = routines.uniq
orphans.delete_if do |orphan|
repeated = routines.find_all{ |routine| routine == orphan }.size
repeated != MagicNumber
end
unused[source] = orphans
end
unused.delete_if{ |h,k| k.empty? }
unused.delete_if{ |source, routines|
routines.delete_if do |o|
routines.inject(0){ |c,r| c + (r == o ? 1 : 0 ) } != MagicNumber
end
routines.uniq!
routines.empty?
}
And I admit freely that the inner inject was stolen from James' try, thanx ;).
Cheers
Robert
···
On Jan 12, 2008 4:24 PM, Bil Kleb <Bil.Kleb@nasa.gov> wrote:
Hi,
I've stalled out on making this simpler, and I'm
seeking help pairing this beast down...
Given unused is a hash of arrays where each array has
repeated elements, make elements unique in each array and
remove all names that are not repeated MagicNumber times.
Finally, remove any hash pairs that hold an empty array.
unused.each do |source,routines|
orphans = routines.uniq
orphans.delete_if do |orphan|
repeated = routines.find_all{ |routine| routine == orphan }.size
repeated != MagicNumber
end
unused[source] = orphans
end
unused.delete_if{ |h,k| k.empty? }
out = unused.inject({}) do |res, (k,v)|
array = v.inject(Hash.new(0)) do |ha, e|
ha[e] += 1
ha
end.inject() do |ar,(ke,cn)|
ar << ke if cn == MagicNumber
ar
end
res[k] = array unless array.empty?
res
end
Kind regards
robert
···
2008/1/12, Bil Kleb <Bil.Kleb@nasa.gov>:
Hi,
I've stalled out on making this simpler, and I'm
seeking help pairing this beast down...
Given unused is a hash of arrays where each array has
repeated elements, make elements unique in each array and
remove all names that are not repeated MagicNumber times.
Finally, remove any hash pairs that hold an empty array.
unused.each do |source,routines|
orphans = routines.uniq
orphans.delete_if do |orphan|
repeated = routines.find_all{ |routine| routine == orphan }.size
repeated != MagicNumber
end
unused[source] = orphans
end
unused.delete_if{ |h,k| k.empty? }
--
use.inject do |as, often| as.you_can - without end
It depends, if space is the issue you are right, if time is an issue
inject is - much to my dispair - not the best idea.
The ideal solution of course is to use Ruby1.9 with count
R.
···
On Jan 12, 2008 11:09 PM, James Gray <james@grayproductions.net> wrote:
oh sorry I forgot, there is another potential performance problem in
my solution.
... look when I called uniq!.
I went for readability but just wanted to be honest about the
tradeoffs in case this is a problem for Bil.
R.