返回顶部

收藏

Divide a set by absolute value

更多
require 'set'

s = [1, 2, 3, -1, -2, -4].to_set

# Divide the set into sets of numbers with the same absolute value.
p s.divide { |x,y| x.abs == y.abs }
# => #<Set: {#<Set: {-1, 1}>,
# =>         #<Set: {2, -2}>,
# =>         #<Set: {-4}>,
# =>         #<Set: {3}>}>

标签:Ruby,集合

收藏

0人收藏

支持

0

反对

0

发表评论