Skip to content

Instantly share code, notes, and snippets.

@nunommc
Created June 5, 2014 14:12
Show Gist options
  • Save nunommc/b9045bbcfc0172d0b59c to your computer and use it in GitHub Desktop.
Save nunommc/b9045bbcfc0172d0b59c to your computer and use it in GitHub Desktop.
AlphaSights Tech Test
log = [
{time: 201201, x: 2},
{time: 201201, y: 7},
{time: 201201, z: 2},
{time: 201202, a: 3},
{time: 201202, b: 4},
{time: 201202, c: 0}
]
def collapse_entries log
new_log = Array.new
log.each do |entry|
add_entry(entry, new_log)
end
new_log
end
def add_entry entry, log
other_value = entry.select {|key, _| key != :time }
one_record = log.select{|e| e[:time] == entry[:time]}.first
if one_record.nil?
one_record = {time: entry[:time]}.merge other_value
log << one_record
else
one_record.merge! other_value
end
end
collapse_entries log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment