From: Josh Cheek on
[Note: parts of this message were removed to make it a legal post.]

On Sun, Feb 7, 2010 at 9:29 PM, Sergio Bayona <bayona.sergio(a)gmail.com>wrote:

> I have a similar challenge:
>
> given: [{"a" => [1, 2]}, {"b" => [3, 4]}]
>
> I need: [{"a" => 1, "b" => 3}, {"a" => 1, "b" => 4}, {"a" => 2, "b" =>
> 3}, {"a" => 2, "b" => 4}]
>
> Any ideas?
>
> Thanks.
> S
> --
> Posted via http://www.ruby-forum.com/.
>
>
Here is a solution I have come up with (http://gist.github.com/297937). I
don't know how you want all edge cases to be handled, I made my best
guesses, you can see them in the tests.

From: Sergio Bayona on
I have a similar challenge:

given: [{"a" => [1, 2]}, {"b" => [3, 4]}]

I need: [{"a" => 1, "b" => 3}, {"a" => 1, "b" => 4}, {"a" => 2, "b" =>
3}, {"a" => 2, "b" => 4}]

Any ideas?

Thanks.
S
--
Posted via http://www.ruby-forum.com/.

From: Jacob Mitchell on
[Note: parts of this message were removed to make it a legal post.]

>
> given: [{"a" => [1, 2]}, {"b" => [3, 4]}]
>
> I need: [{"a" => 1, "b" => 3}, {"a" => 1, "b" => 4}, {"a" => 2, "b" =>
> 3}, {"a" => 2, "b" => 4}]
>
> Any ideas?
>
> Yup, the following method does nearly the same thing, except it takes
arrays. You could either use this method for inspiration, or convert your
desired input into my array of arrays input format and then convert the
output back to your desired format. Hope that helps.

def selections(arrays, results=[Array.new])
return results if arrays.empty?

new_selection_choices = arrays.shift
results_with_new_selections = []

results.each do |prev_selection_set|
new_selection_choices.each do |selection|
results_with_new_selections <<
prev_selection_set.clone.push(selection)
end
end

return selections(arrays, results_with_new_selections)
end

selections([[1, 2], [3, 4]]) #=> [[1, 3], [1, 4], [2, 3], [2, 4]]

From: Sergio Bayona on
Sorry took so long to respond. Your solution worked as expected. Thanks!

SB

Josh Cheek wrote:
> On Sun, Feb 7, 2010 at 9:29 PM, Sergio Bayona
> <bayona.sergio(a)gmail.com>wrote:
>
>> S
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>
> Here is a solution I have come up with (http://gist.github.com/297937).
> I
> don't know how you want all edge cases to be handled, I made my best
> guesses, you can see them in the tests.

--
Posted via http://www.ruby-forum.com/.

 | 
Pages: 1
Prev: [ANN] envjs rubygem 0.1.1
Next: DNote 1.1.3