Permutations in scripting

Update------------------
This problem has been solved. See last two posts.

What would be best practice for creating permutations in coding? More specifically here is what I am doing.

I have three sets of Yes and No buttons. If Yes is clicked it is assigned six different numerical outcomes. If No is clicked it is assigned no outcome.

We will call the first set of Yes and No. Y1 and N1. The second Y2 and N2. The third Y3 and N3.

We will call the first set of numerical outcomes A1 A2 A3 A4 A5 A6. The second B1 B2 B3 B4 B5 B6. The third C1 C2 C3 C4 C5 C6.

So if a user were to press Yes, Yes, NO. The possible outcomes would be:

For Y1:
A1
A2
A3
A4
A5
A6

For Y2:

A1 + B1
A1 + B2
A1 + B3
A1 + B4
A1 + B5
A1 + B6
A2 + B1
A2 + B2
A2 + B3
A2 + B4
A2 + B5
A2 + B6

And etc. through A5.

Y3 would be the same as Y2 because no does not assign a value.

The final outcome is not randomly generated out of this list but the entire list is displayed on the gui.

I can code this but the only way I know how to do it is to list out every single permutation. That would take forever. Is there a faster way to do this? I’m not asking that you write the code but just give me an idea of what best practice for this scenerio would be.

So, the possible number of outcomes is:
0 (3 no’s)
6 (1 Yes)
36 (2 Yeses)
216 (3 Yeses)

I would store an array of values, so let’s say A1 to A6, B1 to B6 and C1 to C6 all are “2” just to keep it simple:

var AValues = new int[] { 2, 2, 2, 2, 2, 2 };
var BValues = new int[] { 2, 2, 2, 2, 2, 2 };
var CValues = new int[] { 2, 2, 2, 2, 2, 2 };
//just so we don't have to create a new array every time
var NoValues = new int[] { 0, 0, 0, 0, 0, 0 };

Now… if “NO” was selected, let’s assume the response value is “0” (for no value).

var ASelected = Q1 == "Yes" ? AValues : NoValues ;
var BSelected = Q2 == "Yes" ? BValues : NoValues ;
var CSelected = Q3 == "Yes" ? CValues : NoValues ;

int score = 0;

for(var i = 0; i < CSelected.length; i++)
{
     for(var j = 0; j < BSelected.length; j++)
     {
          for(var k = 0; k < ASelected.length; k++)
          {
               score += CSelected[i] + BSelected[j] + ASelected[k];
          }
     }
}

Now, if every thing is “0” your final score will be 0. If only the first question was answered as Yes then only the A values will be added, everything else is zero.

I’m assuming this is what you wanted… the final tally adding everything up. If it’s not… let me know and I can help you calculate differently.

Dustin thank you! This is a great start and I have begun to implement it.

However, I don’t want the A values, B values, or C values to add to themselves. In otherwords A+B+C is fine but A+A+A is not.

So the number of possible outcomes that will be displayed in the gui are the same as the total number of possible outcomes.

0 (3 no’s) - shows 0 numbers
6 (Y, N , N) - shows 6 numbers
6 (N, Y, N) - shows 6 numbers
6 (N,N,Y) - shows 6 numbers
36 (Y, Y, N) - shows 36 numbers
36 (Y, N, Y) - shows 36 numbers
216 (Y,Y,Y) - shows 216 numbers

Furthermore, not just the outcome is a permutation but so are the questions. Therefore, we have two different possibilities for creating 36 outcomes- as seen above.

Obviously 216 numbers in a gui will be unwieldly. Many of the numbers will be very close in value- so I probably end up rounding them and showing percentages.

I hope that I have clarified this better. Producing 216 different outcomes is where I am completely stuck.

I’m a little bit lost I think it what you’re asking for… I’ll have to give it some though. Maybe if you could provide a little more clear PseudoCode… The following might seem a little disjointed but I’m just giving you different ways to look at it. :slight_smile:

Basically—

if (Q1 = N and Q2 = N and Q3 = N)
//0 numbers… all answers were “NO”
else if (Q1 = Y and Q2 = N and Q3 = N
OR Q1 = N and Q2 = Y and Q3 = N
OR Q1 = N and Q2 = N and Q3 = Y)

//6 numbers… how are these calculated?

else if (Q1 = Y and Q2 = Y AND Q3 = N
OR Q1 = N and Q2 = Y and Q3 = Y
OR Q1 = Y and Q2 = N and Q3 = Y)

//36 numbers… how are these calculated?

else if (Q1 = Y and Q2 = Y and Q3 = Y)
//216 numbers… how are these calculated?

So you have technically 8 possible scenarios… these could be represented with a bitmask representing your values… where 0 = N and 1 = Y so your possible combinations are (from A to H):
A B C
A 0 0 0
B 1 0 0
C 0 1 0
D 0 0 1
E 1 1 0
F 0 1 1
G 1 0 1
H 1 1 1

Now for the scenarios that match the above:

A) Do nothing… all No’s…
B - D) One Yes, need 6 numbers ( one set)
E - G) Two Yeses, need to add two sets of numbers…resulting in 36
H) Three Yeses, need 216 numbers.

So, we can add the above to figure out how big our resulting set will be…

EXP = AA + AB + AC

IF EXP = 0 THEN
Qs = 0
ELSE
Qs = 1
FOR i = 1 to EXP
Qs = Qs * 6
NEXT
END IF

Now we know how many questions we need so we can appropriately size a result array… Now it’s just a matter of adding the correct numbers which I’ll have to think over…

Ok this is what I have so far:

#pragma strict

// these are the yes and no questions. Yes will equal true and allow there to be values.

var Question1 : boolean = false;
var Question2 : boolean = false;
var Question3 : boolean = false;

// these are the possible outcomes for {1,2,3,4,5,6} respectively

 var Aselected = new Array (0.9,1.75,1.77,1.79,1.8,3.5);
 var Bselected = new Array (0.9,1.75,1.77,1.79,1.8,3.5);
 var Cselected = new Array (0.9,1.75,1.77,1.79,1.8,3.5);



function Start () 
{

    var values1 = [Aselected];
   
 // if only one yes button is pressed   
      
    if (Question1 == true || Question2 == false || Question3 == false){
   		 for(var outcome1 : Array in values1){
       	 	print (outcome1);
   		 }
   	}	 
   	else if (Question1 == false || Question2 == true || Question3 == false){
   		 for(var outcome2 : Array in values1){
       	 	print (outcome2);
   		 }	
   	}	 
   	else if (Question1 == false || Question2 == false || Question3 == true){
   		 for(var outcome3 : Array in values1){
       	 	print (outcome3);
   		 }
   	}	 
   		 
 // if 2 yes buttons are pressed
 
 	if (Question1 == true || Question2 == true || Question3 == false){
   		 for(var outcome4 : Array in values1){
       	 	print (outcome4);
   		 }
   	}	 
   	else if (Question1 == false || Question2 == true || Question3 == true){
   		 for(var outcome5 : Array in values1){
       	 	print (outcome5);
   		 }	
   	}	 
   	else if (Question1 == true || Question2 == false || Question3 == true){
   		 for(var outcome6 : Array in values1){
       	 	print (outcome6);
   		 }
   	}
 
// if 3 yes buttons are pressed

    if (Question1 == true || Question2 == true || Question3 == true){
   		 for(var outcome7 : Array in values1){
       	 	print (outcome7);
   		 } 	
   	} 	   		 	 	 	   		 	 	   		 	 	 	 
}

However, the only way that I can get the arrays to add together is with concatenate. This just makes the array of 6 into an array of 12.

What I want to do is 0.9 + 0.9, 0.9+ 1.75, 0.9+ 1.77, 0.9+ 1.79, 0.9 + 1.8, 0.9 + 3.5 then the same for every float in the array.

Any ideas??

foreach (int i in A){
  foreach (int j in B){
    foreach (int k in C){
      print(i+j+k);
    }
  }
}

Would print all combos of all ints.

hpjohn based on your comment here are my modifications:

#pragma strict

 

// these are the yes and no questions. Yes will equal true and allow there to be values.

var Question1 : boolean = false;

var Question2 : boolean = false;

var Question3 : boolean = false;

 

// these are the possible outcomes for {1,2,3,4,5,6} respectively

 

 var Aselected = new Array (0.9,1.75,1.77,1.79,1.8,3.5);
 var Bselected = new Array (0.9,1.75,1.77,1.79,1.8,3.5);
 var Cselected = new Array (0.9,1.75,1.77,1.79,1.8,3.5);

 

 

 

function Start () 

{

 
 //   var values1 = [Aselected];

   

 // if only one yes button is pressed   

      

    if (Question1 == true || Question2 == false || Question3 == false){

         for(var outcome1 : float in Aselected){

            print (outcome1 + outcome1);

         }

    } 
}

However, this still does not work it only gives one permutation not all six. For example if the two arrays were as follows:
(A, B, C, D, E, F)
(A, B, C, D, E, F)

using the for each only gives 6 out comes

AA, BB, CC, DD, EE, FF

what I need is the 36 outcomes

AA AB AC AD AE AF
BA BB BC BD BE BF
CA CB CC CD CE CF
DA DB DC CD CE DF
EA EB EC ED EE EF
FA FB FC FD FE FF

Dustin,

After looking again at this I think that this is the answer and I was just misreading the code!!! I will work through it now code it and let you know!

Ok! I am very close here… The only problem is that the booleans are not working correctly. Can anyone help me trouble shoot?

#pragma strict

var Question1 : boolean = false;
var Question2 : boolean = false;
var Question3 : boolean = false;

var AValues : float[]; 
var BValues : float[];
var CValues : float[];
var arrayLength = 6;
var result = new Array ();

function Start () {

 // if only one yes button is pressed   

    if (Question1 == true || Question2 == false || Question3 == false){
   		for(var a = 0; a < arrayLength; a++){
        	var one = AValues[a];
			result.Push(one); 
		}    
    }    

    else if (Question1 == false || Question2 == true || Question3 == false){
   		for(var b = 0; b < arrayLength; b++){
        	var two = AValues[b];
			result.Push(two); 
		}    
    }    

    else if (Question1 == false || Question2 == false || Question3 == true){
   		for(var c = 0; c < arrayLength; c++){
        	var three = AValues[c];
			result.Push(three); 
		}       
    }    
        
 // if 2 yes buttons are pressed

    else if (Question1 == true || Question2 == true || Question3 == false){
   		for(var d = 0; d < arrayLength; d++){

    		for(var e = 0; e < arrayLength; e++){

              	var four = AValues[d] + BValues[e];
				result.Push(four); 
       
     		}
		}    
    }    

    else if (Question1 == false || Question2 == true || Question3 == true){
   		for(var f = 0; f < arrayLength; f++){

    		for(var g = 0; g < arrayLength; g++){

              	var five = AValues[f] + BValues[g];
				result.Push(five); 
       
     		}
		}    
    }    


    else if (Question1 == true || Question2 == false || Question3 == true){
   		for(var h = 0; h < arrayLength; h++){

    		for(var l = 0; l < arrayLength; l++){

              	var six = AValues[h] + BValues[l];
				result.Push(six); 
       
     		}
		}    
    }

// if 3 yes buttons are pressed

   else if (Question1 == true || Question2 == true || Question3 == true){
   
   		for(var i = 0; i < arrayLength; i++){

    		for(var j = 0; j < arrayLength; j++){

        		for(var k = 0; k < arrayLength; k++){

              		 var seven = AValues[i] + BValues[j] + CValues[k];
					 result.Push(seven); 
          		}

     		}

		}
  	 }


print (result);


}

The above script works but I confused || (or) for (and). Changing all of the || to gives me all of the permutations!!!