Solved

Permutations of multiple string arrays' elements

Posted on 2003-12-10
11
556 Views
Last Modified: 2012-05-04
I am working on an insurance system web app. The system allows users to set up price quotes based on a number of factors. The quotes must be made for all combinations of selected factors. So, if I’m making a quote, the factors that I choose to base it on are:

 

1.       Age

2.       Sex

3.       Smoker

 

Now, for each factor, I must define options, like so:

 

1. Age
     a.       21-34
     b.       35-64
     c.       65-99

2. Sex
     a.       Male
     b.       Female

3. Smoker
     a.       Non-Smoker
     b.       Less than 1 pack per day
     c.       More than 1 pack per day

 

OK, now comes the hard part. I need to generate every possible permutation of these three factors individual options, like so:

 

1.       Male, 21-34, Non-Smoker

2.       Male, 35-64, Non-Smoker

3.       Male, 65-99, Non-Smoker

4.       Male, 21-34, Less than 1 pack per day

5.       Male, 35-64, Less than 1 pack per day

6.       Male, 65-99, Less than 1 pack per day

7.       Male, 21-34, More than 1 pack per day

8.       Male, 35-64, More than 1 pack per day

9.       Male, 65-99, More than 1 pack per day

10.   Female, 21-34, Non-Smoker

11.   Female, 35-64, Non-Smoker

12.   Female, 65-99, Non-Smoker

13.   Female, 21-34, Less than 1 pack per day

14.   Female, 35-64, Less than 1 pack per day

15.   Female, 65-99, Less than 1 pack per day

16.   Female, 21-34, More than 1 pack per day

17.   Female, 35-64, More than 1 pack per day

18.   Female, 65-99, More than 1 pack per day

 

Now, each of these factors can have any number of options (except for Sex, which can only be male or female.)

I have wasted 2 days on this problem. If anybody can help me, you'd be saving the rest of my hair. Thanks.
0
Comment
Question by:henningero
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9912883
>> generate every possible permutation of these three factors individual options.

This sounds too abstract. What exactly you need? For example, this is pseudo-code for printing all permutations:

for each 3 years from 0 to 1000
{
    for each sex
    {
        for each smoking state
        {
              print line
        }
    }
}

0
 

Author Comment

by:henningero
ID: 9913069
I basically need every possible combination of each arrays elements. So, if I have 3 arrays, each with 2 elements, i need the combinations of each element of each array.

string[] gender = new string{"Male", "Female"}

string[] smoking = new string{"Non-Smoker", "Smoker"}

string[] age = new string{"21-34", "34-99"}

I need the combinations like so:

Male, Non-Smoker, 21-34
Male, Non-Smoker, 34-99
Male, Smoker, 21-34
Male, Smoker, 35-99
Female, Non-Smoker, 21-34
Female, Non-Smoker, 34-99
Female, Smoker, 21-34
Female, Smoker, 35-99

The problem is, I don't know how many elements each array will have (except for gender, which will only have Male or Female). Smoking can be Non-Smoker, Smoker, Less than 1 pack per day, More than 1 pack per day, Not in last 5 years, or any user input. Age can be any number of user defined age bands, like 21-34, 35-64, 65-99 in any amounts and combination.

I do know that there will be at least one array (either age, sex, or smoking) and no more than three arrays (age, sex AND smoking). The arrays sized can all be different, except gender(only Male or Female). I hope this is clear enough.
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 500 total points
ID: 9913323


very clear.

To make it a little more flexible, I am going to use ArrayLists



ArrayList sex = new ArrayList();
ArrayList smoke= new ArrayList();
ArrayList age= new ArrayList();

// load appropriate types here
sex.Add("male");
sex.Add("female");

etc...

ArrayList myData = new ArrayList();

for(int i=0;i<sex.count;i++)
   for(int j=0<j<smoke.count;j++)
      for(int k=0;k<age.count;k++)
          myData.Add(sex[i] + "," + smoke[j] + "," + age[k]);


for(int x=0;x<myData.Count;x++)
    Console.Writeline(myData[x].ToString());


That's it.


If you want to know how many possible combinations there are it is simply multiplying each factor by the next criteria. That is: age.count * smoke.count * sex.count.




0
 
LVL 7

Expert Comment

by:TransBind
ID: 9913689
this is in javascript solution but you can adopt it to your needs
<script language='javascript'>

var Gender = new Array();
Gender[0] = "Male";
Gender[1] = "Female";

var Smoke = new Array();
Smoke[0] = "Non-Smoker";
Smoke[1] = "Smoker";

var Age = new Array();
Age[0] = "21-34";
Age[1] = "34-99";
Age[2] = "35-99";
Age[3] = "45-99";

function fnPermutations()
{
      for(a=0; a<Gender.length; a++)
      {
            for(b=0; b<Smoke.length; b++)
            {
                  for(c=0; c<Age.length; c++)
                  {

                        document.write(Gender[a] + " , " + Smoke[b] + " , " + Age[c]);
                        document.write("<br>");
                  }
            }      
      }
}

</script>
0
 
LVL 7

Expert Comment

by:TransBind
ID: 9913690
oups I didn't know the solution was already posted
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:henningero
ID: 9913691
Excellent. Thank you very much _TAD_! You are truly a man among boys!
0
 
LVL 7

Expert Comment

by:TransBind
ID: 9913697
< ---- slow
0
 
LVL 11

Expert Comment

by:Agarici
ID: 9913706
i suppose the solution _TAD_ gave you works just fine, however with large sets it can be time consuming.

so just as an idea: if you put  the three(or more) kinds of atributes in 3(or more) database tables you will only have to do a 'cross join' because what you need to do is campute the cartesian product of the three sets of values.

 
A.
0
 

Author Comment

by:henningero
ID: 9913707
Thanks, TransBind. I think you just missed it. But an excellent answer nonetheless! Thanks to you both!
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9913852


Agarici>

  That did actually cross my mind (using a cartesian join on database tables).  However, in my experience it is really just as quick to load several arrays as long as the data is not too complex and the amount of data is relatively trivial.


if henningero were to ultimately have more than... let's say 10,000 distinct permutaions then the solution i provided would work, but it would be extremely slow (perhaps 6 to 12 seconds of load time).

In a realistic setting, I don't foresee henningero's final data payload to be more than 50 or 100 distinct items (200 items on the high side).  Hence the quick and dirty, brute force method I provided.

but I like the way you think... efficiency is right near the top of the list when it comes to writing code



0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9913858


Thanks for the points!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WKHTMLTOPDF - --disable-smart-shrinking not working 10 36
Syntax Error 2 46
Error on link 14 38
How useful is the free version of Selenium? 3 12
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now