Send to a Friend

gorillapaws's avatar

Programmers: Is this puzzle a "named problem?" and can you help me with it?

I’m attempting to write a function that takes as an input an array of key-value pairs of arbitrary number, with the keys being strings and the values being an array of strings for each key, also of arbitrary number. e.g.

[“FOO” : [“a”, “b”, “c”, “d”…],
“BAR” : [“X”, “Y”, “Z”, “Q”, …],
“BAZ” : [“3”, “7”, “13”, “42”, “0”, …], …]

The output would be an array of arrays of key-value pairs (aka an array of dictionaries) that represent all possible combinations of keys/values from the input. For example:

[[“FOO” : “a”, “BAR” : “X”, “BAZ” : “3”],
[“FOO” : “b”, “BAR” : “X”, “BAZ” : “3”],
[“FOO” : “c”, “BAR” : “X”, “BAZ” : “3”],
[“FOO” : “d”, “BAR” : “X”, “BAZ” : “3”],
[“FOO” : “a”, “BAR” : “Y”, “BAZ” : “3”], …]

One way to think of the problem is you have a briefcase with an arbitrary number of tumbler locks that each have an arbitrary number of symbols on each tumbler, write a function that will produce an exhaustive list of all possible combinations to the locks.

Is this puzzle a “named” problem like the “traveling salesman” problem?

When trying to think of the solution, I was considering using a tree structure to model the data with the number of entries in the array for each key representing branching width and each key representing depth of the tree, and then using recursion to walk up the tree and assemble the solution arrays. My solution started to become a lot more complicated though and I suspect there’s a much simpler approach to this that I’m not seeing.

Thanks for any help you can offer!

Topics: ,

Using Fluther

or

Using Email

Separate multiple emails with commas.
We’ll only use these emails for this message.

Mobile | Desktop


Send Feedback   

`