Ruby 看看这道题怎么做, $SAFE=4

sandect · 2014年03月20日 · 最后由 david_w 回复于 2014年03月24日 · 2059 次阅读

http://www.alphasights.com/careers/apply/hong-kong/ruby-on-rails-developer

A local variable named log contains an array of hashes with timestamped events like so 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} ]

Please collapse the log by date into an array of hashes containing one entry per day [ {time: 201201, x: 2, y: 7, z: 2}, {time: 201202, a: 3, b: 4, c: 0}, ]

Ordering in the input is not defined. Ordering in the result is not important. Do not rely on the names of the hash keys other than :time. The field below executes your code in a Sandbox with $SAFE=4, so you can't define new classes, use global variables, etc.

log.group_by {|l| l[:time]}.values.map {|arr| arr.inject &:merge}

这道题,用 python 怎么实现?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号