Skip to content

Instantly share code, notes, and snippets.

@jvnill
Forked from jp/netengine_golf.rb
Last active December 10, 2015 00:19
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 jvnill/4350598 to your computer and use it in GitHub Desktop.
Save jvnill/4350598 to your computer and use it in GitHub Desktop.
require 'rspec'
require 'json'
require 'active_support/all'
require 'pry'
def input
[
{lastmodificationdate:"2012-11-16",userproject:'FCBIZ',totalcost:10},
{lastmodificationdate:"2012-11-16",userproject:'LMS',totalcost:20},
{lastmodificationdate:"2012-11-16",userproject:'NetEngine',totalcost:52},
{lastmodificationdate:"2012-11-17",userproject:'FCBIZ',totalcost:11},
{lastmodificationdate:"2012-11-17",userproject:'LMS',totalcost:22},
{lastmodificationdate:"2012-11-17",userproject:'NetEngine',totalcost:53},
{lastmodificationdate:"2012-11-18",userproject:'FCBIZ',totalcost:34},
{lastmodificationdate:"2012-11-18",userproject:'LMS',totalcost:32},
{lastmodificationdate:"2012-11-18",userproject:'NetEngine',totalcost:55}
]
end
def desired_output
[
{ :lastmodificationdate => "2012-11-16", "FCBIZ" => 10, "LMS" => 20, "NetEngine" => 52 },
{ :lastmodificationdate => "2012-11-17", "FCBIZ" => 11, "LMS" => 22, "NetEngine" => 53 },
{ :lastmodificationdate => "2012-11-18", "FCBIZ" => 34, "LMS" => 32, "NetEngine" =>55}
]
end
class NetEngineGolf
def jim(input)
input.group_by { |a| a[:lastmodificationdate] }.map do |date, values|
values.inject({ lastmodificationdate: date }) do |hash, value|
hash.merge value[:userproject] => value[:totalcost]
end
end
end
end
describe NetEngineGolf do
describe "#jim" do
it "turns the input into the output" do
output = NetEngineGolf.new.jim_1(input)
output.should == desired_output
end
end
end
@lclemence
Copy link

Thanks!! Exactly what I needed and in few minutes... impressive!!

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