Skip to content

Instantly share code, notes, and snippets.

@ryenus
Created August 5, 2012 02:19
Show Gist options
  • Save ryenus/3261155 to your computer and use it in GitHub Desktop.
Save ryenus/3261155 to your computer and use it in GitHub Desktop.
trying gist 2700553

Trying to turn this into that, why the box keeps red for the code below?

log.reduce([]) do |s,e|
  r = s.last
  (r = {}; s << r) if r.nil? or r[:time] != e[:time]
  r.merge! e
  s
end

It works fine though

$ irb
>> 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}
>> ]
=> [{:time=>201201, :x=>2}, {:time=>201201, :y=>7}, {:time=>201201, :z=>2}, {:time=>201202, :a=>3}, {:time=>201202, :b=>4}, {:time=>201202, :c=>0}]
>> log.reduce([]) do |s,e|
?>   r = s.last
>>   (r = {}; s << r) if r.nil? or r[:time] != e[:time]
>>   r.merge! e
>>   s
>> end
=> [{:time=>201201, :x=>2, :y=>7, :z=>2}, {:time=>201202, :a=>3, :b=>4, :c=>0}]
@tadast
Copy link

tadast commented Aug 6, 2012

@ryenus, the quiz check uses a slightly different test data. Just play around with the order of hashes in the log array and you'll see that your method fails to solve the task in certain cases.

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