Skip to content

Instantly share code, notes, and snippets.

@gavinheavyside
Created July 19, 2012 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavinheavyside/3146125 to your computer and use it in GitHub Desktop.
Save gavinheavyside/3146125 to your computer and use it in GitHub Desktop.
a solution to the alphasights job quiz
# As seen in Ruby Weekly newsletter http://rubyweekly.com/archive/102.html
# http://www.alphasights.com/apply/ruby-developer-london
by_time = log.reduce(Hash.new{|hash,key| hash[key] = {}}) do |memo, val|
memo[val[:time]].merge!(val.reject{|k,v| k == :time})
memo
end
by_time.map{|k,v| v.merge(time: k)}
@peterc
Copy link

peterc commented Jul 19, 2012

OK, I gave it a go. My attempt:

log.group_by { |i| i[:time] }.map { |_,i| i.reduce(:merge) }

@gavinheavyside
Copy link
Author

Very nice, I'd completely overlooked group_by! Your solution is much faster too:

       user     system      total        real
hgavin  1.810000   0.060000   1.870000 (  1.862071)
peterc  0.770000   0.020000   0.790000 (  0.795652)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment