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? }

Here's my solution, untested:

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

-- 
use.inject do |as, often| as.you_can - without end