how do I get all combinations of a set of constants?

Suppose we have six constants: 1 through 6.

I would like to have a formula to get all combinations of of them,

I give an example of what I mean:

1 and 2 and 3 and 4 and 5 and 6
1 and 2 and 3 and 4 and 5, but not 6
1 and 2 and 3 and 4, but not 5 and 6
...
2 and 3 and 4 and 5 and 6, but not 1
3 and 4 and 5 and 6, but not 1 and 2
...
1 and 3 and 4 and 5 and 6, but not 2
1 and 4 and 5 and 6, but not 2 and 3
...
...
I Think you've got it.

Lastly, I'd put this in a php script to get the correct combination

if(1 and 2 and 3 and 4 and 5 and 6) {  do this  }
if(1 and 2 and 3 and 4 and 5)  {  do that  }

For me this is far over my training. Could you please lead me thorugh this.

Thanks.
Lennart EricsonAmateurAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
Counting cases where we don't care about some of the conditions would give 3^6 combinations, but some of them would then be overlapping, since
if(1 and 2 and 3 and 4 and 5)  covers both if(1 and 2 and 3 and 4 and 5 and 6) and if(1 and 2 and 3 and 4 and 5 and not 6)
so there would still be no need for more than of 2^6 separate if statements.
Which is still a large enough number that you'd probably prefer to use a table rather than individual if statements.
0
 
Ray PaseurConnect With a Mentor Commented:
Have you tried to write any of the code for this?  If so, please show us what you've started so we don't waste your time repeating anything.

Is there a business reason for this?  It sounds so theoretical and academic that I think if you explain the business logic behind your application requirement we might be able to suggest something that would make sense.
0
 
Gregory MillerConnect With a Mentor General ManagerCommented:
That is a lot of combinations... 6! or six factorial = 1*2*3*4*5*6=720
That makes for a whole lot of IF statements...
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Ray PaseurCommented:
With 720 if() statements, you might want to consider a switch construct.  But seriously, please show us what you've tried and what kind of output you're getting.  I am sure we can help.
0
 
Ray PaseurCommented:
This might be one way of looking at the problem.

<?php // RAY_combinations_all_ordered_subsets.php
error_reporting(E_ALL);
echo "<pre>";


// PROBLEM: FIND ALL ORDERED SUBSETS OF A CHARACTER SET
// EXAMPLE: IN "ABC" IT MUST FIND "A", "AB", "BA", ETC.


// FUNCTION TO GENERATE THE COMBINATIONS OF CHARACTERS IN A STRING.
// OUTPUT IS AN ARRAY OF COUNT = strlen() FACTORIAL
// ADAPTED FROM http://cogo.wordpress.com/2008/01/08/string-permutation-in-php/
function string_combinations($str)
{
    if (strlen($str) < 2) return array($str);

    $combos = array();
    $endpop = substr($str, 1);

    // RECURSE
    foreach (string_combinations($endpop) as $permutation)
    {
        $length = strlen($permutation);
        for ($i = 0; $i <= $length; $i++)
        {
            $combos[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
        }
    }
    return $combos;
}


// GENERATE THE COMBINATIONS OF ELEMENTS IN A ONE-DIMENSIONAL ARRAY
function array_combinations($arr)
{
    $return = array();

    // GET THE NUMERIC KEY RANGE OF THE INPUT ARRAY
    $str = implode('', range(0, count($arr)-1));

    // GET THE KEY COMBINATIONS
    $combos = string_combinations($str);

    // ITERATE OVER THE COMBINATIONS
    foreach ($combos as $combo)
    {
        $out = array();
        while (strlen($combo))
        {
           $key   = substr($combo,0,1);
           $combo = substr($combo,1);
           $out[] = $arr[$key];
        }
        $ndx = implode(NULL, $out);
        $return[$ndx] = $out;
    }
    return $return;
}


// DEMONSTRATE THE CODE
$abc = 'ABCDEF';
$abc = str_split($abc);
$cnt = count($abc) - 1;

// GET ALL LARGEST COMBINATIONS IN ARRAY FORMAT
$out = array_combinations($abc);
$out = array_keys($out);

// REDUCE THE STRING LENGTHS TO FIND SMALLER COMBINATIONS
while ($cnt)
{
    foreach ($out as $key)
    {
        $key = substr($key, 0, $cnt);
        $key = str_split($key);
        sort($key);
        $key = implode(NULL, $key);
        $new[$key] = $key;
    }
    $cnt--;
}

// WITH EACH OF THE REDUCED STRINGS, ADD ALL COMBINATIONS
foreach ($new as $abc)
{
    $arr = array_combinations(str_split($abc));
    $out = array_merge($out, array_keys($arr));
}

// SHOW THE WORK PRODUCT
sort($out);
echo "FOUND " . number_format(count($out)) . " COMBINATIONS";
echo PHP_EOL;
print_r($out);

Open in new window

0
 
phoffricConnect With a Mentor Commented:
>>  6! or six factorial = 1*2*3*4*5*6=720
But in the question, ordering of the conditions doesn't matter, so 720 is too high.

Is
if not any, then do this
one of the options? If it is then you have 2^6 = 64 conditions. (Think of a 6 bit flag where if a bit is set, then that condition is required, and if not set, then that condition must be false.)

Now, do you actually have 64 do this or that? If not, group the conditions into families associated with each of the  do this or that.
0
 
phoffricCommented:
>> if(1 and 2 and 3 and 4 and 5)  {  do that  }
Just noticed this condition which is not the same as:
     if(1 and 2 and 3 and 4 and 5 and not 6)  {  do that  }

Your previous examples always had 6 conditions in them, but this one has only 5. Is that your intent?
0
 
phoffricCommented:
Is this question related to coursework - homework or otherwise? If so, then you should provide us with what you have and ask a specific question about a problem since EE policy says that we provide guidance but not solutions to academic type questions.
0
 
d-glitchConnect With a Mentor Commented:
The max number of combinations is 2^6 = 64, not 6! = 720.

The way to enummerate, test, and act on them is with a 6-bit binary field or number.

000000  ==> no conditions
000001  ==> condition 1
000010  ==> condition 2
000011  ==> conditions 1 and 2
. . .
111110 ==> all but 1
111111 ==> all  six conditions
0
 
phoffricCommented:
>> The max number of combinations is 2^6 = 64, not 6! = 720
Thanks for agreeing with me. But as I asked the author, if his last test of
if(1 and 2 and 3 and 4 and 5)  {  do that  }
is really what he meant, then there are more than 64 combined conditions to consider.
0
 
Lennart EricsonAmateurAuthor Commented:
Thanks for all your replies. This is far beyond what I had in mind. I appreciate your willingness to help. I want to Close this matter now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.