0

I'm trying to work out the best approach to producing a list of valid cases, based on selections made in an arbitrary number of sections. Perhaps it's not really an algorithm, rather just advice on how to effectively iterate, but it seems like an algorithm question to me. Please correct me if I am wrong. The implementation is actually in Javascript, but it could equally apply for any language, hence the non-language-specific question.

So there are a number of sections, each having various choices, and data can be in any number of choices for each section.

If no choices are made in a section, all data is allowed through for that section. If choices are made then data must have one or more of those choices.

So for example, with:

Section: vacancy types
Choices: 1, 3

Section: exhibitor categories
Choices: 1, 5, 9

I want to come out with the following valid cases:

1,1
1,5
1,9
3,1
3,5
3,9

As I said, if no choices are made, all data should be allowed through, which is where I struggle most with working out the iteration. But also I would like a generic iteration that can be used for any number of sections, rather than just two.

I'm sure this is quite simple, and no doubt my language here is not ideal (should have listened more closely in Computer Science lessons) but how do I set up my iteration to give me the above?

I have no idea how to find the right resources to read up on this, so just a relevant link or two would be an acceptable answer, although of course I am also interested in specific answers please.

Thanks.

  • 1
    I have a hard time understanding your question. You define sections and choices and give and example where you produce the cartesian product of two sets of choices which seems trivial. But what is _data_ and _data allowed through for that section_? – Martin Liversage Sep 21 '13 at 10:00
  • Sorry for not explaining it very well. I am analysing data against these choices, and the "valid cases" are the possible combinations that the data could have to be "allowed through" the filter that I am using this as. Sorry I reworded my question a few times and no doubt it could have been better worded. You have given me what I need to know, I want the Cartesian product and an algo is given here: http://stackoverflow.com/a/2419399/2493235 I can't accept your comment as an answer. If you would like to make an answer along the lines of my needing an algo for Cartesian product, I will accept it –  Sep 21 '13 at 10:56

1 Answers1

1

Your valid cases are the cartesian product of the choices in the two sections. You can compute a higher order product which will yield a sequence of combinations of choices with N elements where N is the order of the product (the number of sections).

The most natural algorithm is to use recursion, but iterative algorithms are also possible as you have discovered yourself. I had some fun using LINQ as explained by Eric Lippert.

Community
  • 1
  • 1
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256