1

What is a good way to get from this:

['a','b','c',['d1','d2']]

to this:

[['a','b','c','d1']['a','b','c','d2']]

another example, from this:

[['a1','a2'],'b',['c1','c2']]

to this:

[['a1','b','c1'],['a1','b','c2'],['a2','b','c1'],['a2','b','c2']]

edit 1:

Sorry for the confusion and thanks for response so far, the individual contents of the array items doesn't matter but the order must be preserved. The method needs to work for both example because the nested array can be in any position of the outer array, and the nested array can have more the 2 elements.

It's sort of like a regex with multiple or conditions

ab(c|d) 

expand to match abc and abd

1
  • Do you want to combine elements of first array into some random way OR there is some rules that you want to follow? Commented Nov 18, 2013 at 12:12

3 Answers 3

1

It is a bit hard to know exactly what you want, but this produces something quite similar:

# Create a list:
a = [['a1','a2'],'b',['c1','c2']]

# Split it into sub-arrays and single values:
list, other = a.partition{|x|x.is_a? Array}

# Split the array in order to get the product:
list_first, list_rest = list.pop, list

# Get the product  and add the others_values:
p list_first.product(*list_rest).map{|list| list+other}
#=> [["c1", "a1", "b"], ["c1", "a2", "b"], ["c2", "a1", "b"], ["c2", "a2", "b"]]
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the partition and product methods usage, it was helpful.
1

1st:

arr1 = ['a','b','c',['d1','d2']]
*a, b = arr1
# ["a", "b", "c", ["d1", "d2"]]

a
# ["a", "b", "c"]
b
# ["d1", "d2"]

b.map{|x| a+[x]}
# [["a", "b", "c", "d1"], ["a", "b", "c", "d2"]]

and 2nd:

a, b, c = [["a1", "a2"], "b", ["c1", "c2"] ]

a.product c
#=> [["a1", "c1"], ["a1", "c2"], ["a2", "c1"], ["a2", "c2"]]
a.product(c).map{|x| x<<b}
#=> [["a1", "c1", "b"], ["a1", "c2", "b"], ["a2", "c1", "b"], ["a2", "c2", "b"]]

#or little less readable:
a.product(c).map{|x| [ x[0], b, x[1] ]}
# [["a1", "b", "c1"], ["a1", "b", "c2"], ["a2", "b", "c1"], ["a2", "b", "c2"]]

1 Comment

nice. a bit shorter version of the 1st: ['a','b','c',['d1','d2']].pop.map{|x| a+[x]}
0

hirolau got me really close, here is what I ended with so the order is preserved:

# given a sample list
sample = [['a','b'],'c','d',['e','f'],'g',['h','i']]

# partition out the arrays
arrs, non_arrays = sample.partition {|sample| sample.is_a? Array}

# work out all possible products  
first_elem, *the_rest = arrs
products = first_elem.product(*the_rest)

# finally swap it back in to get all valid combinations with order preserved
combinations = []

products.each do |p|
  combinations << sample.map {|elem| elem.is_a?(Array) ? p.shift : elem} 
end

# combinations
=> [["a", "c", "d", "e", "g", "h"],
 ["a", "c", "d", "e", "g", "i"],
 ["a", "c", "d", "f", "g", "h"],
 ["a", "c", "d", "f", "g", "i"],
 ["b", "c", "d", "e", "g", "h"],
 ["b", "c", "d", "e", "g", "i"],
 ["b", "c", "d", "f", "g", "h"],
 ["b", "c", "d", "f", "g", "i"]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.