新手问题 从每个数组中选一个,组合成一个新的数组的内建方法。

Alexander · 2013年04月25日 · 最后由 Alexander 回复于 2013年04月25日 · 2467 次阅读

请问,实现下面功能的内建方法有吗?

def combine *arrays
  if arrays.length == 0
    yield []
  else
    arrays.pop.each do |n|
      combine *arrays do |x|
        yield x.push n
      end
    end
  end
end

combine %w{a b}, %w{A B} do |x| p x end
# result
# ["a", "A"]
# ["b", "A“]
# [”a", "B"]
# ["b", "B"]

zip 有点点像。但只能来一轮。

a,b = [%w(a b), %w(A B)]
a.product b
4 楼 已删除

#3 楼 @Ddl1st good job. 排列组合都不成,这个应该叫乘积

#6 楼 @Ddl1st 额,好吧,犀利

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