Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def myproduct(*arrays)
- return [] if arrays.empty?
- return arrays[0].collect{|elem| [elem]} if arrays.size == 1
- head = arrays[0]
- tail = arrays.drop(1)
- head.inject([]) do |x1,y1|
- myproduct(*tail).inject(x1) do |x2,y2|
- x2 << [y1] + y2
- end
- end
- end
- p myproduct([1,2],[3,4]) # => [[1, 3], [1, 4], [2, 3], [2, 4]]
- p myproduct([1],[2,3,4]) # => [[1, 2], [1, 3], [1, 4]]
- p myproduct([1,2],[3,4],[]) # => []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement