Generate a combination of numbers

2020-02-13 05:22发布

I have a PHP page with two variables: $nbRank and $nbNumeric. Depending of these two variables, I want to generate an array containing all combinations existing. For example:

If $nbRank = 3 and $nbNumeric = 2 I would have:

0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 2 2
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 1 2
2 2 0
2 2 1
2 2 2

So, I create different loop and formulas to get the final result, but it doesn't works. This is what I did :

$result = array();

$nbIntRank = 0;
$nbIntNumeric = 0;
$nbRank = array();
$nbNumeric = array();

$nb_rangs = 3;
$nb_chiffres = 2;

for ($i = 1; $i <= $nb_rangs; $i++){
    $nbRank[$i] = 0;
}

$nbIntRank = count($nbRank);

for ($i = 0; $i <= $nb_chiffres; $i++){
    $nbNumeric[$i] = $i;
}

$nbIntNumeric = count($nbNumeric);

$algo = ($nb_rangs * ($nb_chiffres + 1)) * ($nb_rangs * ($nb_chiffres + 1));
$nbLine = $algo / ($nb_rangs);

$occ = 0;
for ($i = 0; $i < $nbLine; $i++){
    foreach ($nbRank as $nbrItem => $nbrValue){
        $result[$i][] = $nbrValue;
        $occ++;
    }
}

echo '#############<br />';
echo '### DATAS ###<br />';
echo '#############<br /><br />';

echo '- Nb Elements : '.$algo.'<br />';
echo '- Nb Lines : '.$nbLine.'<br />';
echo '- Nb Valuable Occurency : '.$occ.'<br />';

echo '<br /><hr /><br />';
echo '##############<br />';
echo '### PARSER ###<br />';
echo '##############<br /><br />';

echo '<pre>';
var_dump($result);
echo '</pre>';

I managed to create my final array with empty values (81 values, in 27 lines of 3 elements) but it only contains 0.

3条回答
唯我独甜
2楼-- · 2020-02-13 05:39

Here's a recursive solution:

$nbRank = 3;
$nbNumeric = 2;

function getCombinations ($length, $min, $max, $aStartingCombinations)
{
    if ($length == 1)
    {
        return range ($min, $max);
    }

    $final = array ();
    foreach (getCombinations ($length - 1, $min, $max, $aStartingCombinations) as $combination)
    {
        for ($i = $min; $i <= $max; $i++)
        {
            $final [] = $combination . $i;
        }
    }
    return $final;
}

print_r (getCombinations ($nbRank, 0, $nbNumeric, array ()));
查看更多
三岁会撩人
3楼-- · 2020-02-13 05:53
$nbRank = 3;
$nbNumeric = 2;

foreach (range(0, base_convert(str_pad('', $nbRank, $nbNumeric), $nbNumeric+1, 10)) as $i) {
  echo str_pad(base_convert($i, 10, $nbNumeric+1), 3, 0, STR_PAD_LEFT) . PHP_EOL;
}

Simple idea: What you want is every number from 0 to X with base $nbNumeric, thus we just convert the maximum number to base 10, iterate over it with the common 10-based operators, and convert it back to base $nbNumeric again.

Probably more readable, but in fact exactly the same

$nbRank = 3;
$nbNumeric = 2;

// Top is "base_convert(222, 3, 10);" and therefore the upper limit
$top = base_convert(str_pad('', $nbRank, $nbNumeric), $nbNumeric+1, 10);
for ($i = 0; $i <= $top; $i++) {
    echo str_pad(base_convert($i, 10, $nbNumeric+1), 3, 0, STR_PAD_LEFT) . PHP_EOL;
}
查看更多
倾城 Initia
4楼-- · 2020-02-13 05:55

You indicated you'll be fine with pseudo-code.. Sorry I cannot offer specific correction for your php code [if these answers appear - they might be more educating], but I'd chose a recursive solution for this problem.

In each level of the recursion, try all possibilities, and call the same function to find all combinations of one smaller size.

Pseudo-Code:

findCombinations(range,size,sol,resultList):
  if (size ==0): #base clause
     resultList.append(copy(sol)) #making a copy of sol and appending it as a solution
     return
  for each i in (0,range):
     sol.append(i)
     findCombinations(range,size-1,sol,resultList) #recursive invokation, with smaller size
     sol.deleteLast() #clean up environment before next calls

Invoke with findCombinations(3,3,[],resultList) where [] is just empty list, and resultList will hold the list of combination when the algorithm is done. this invokation will get all combinations of size 3 with elemenets 0,1,2.

Complexity note: The number of possibilities is growing exponentially [O(rangesize)], so if you try to invoke it with 20,20 for instance - it might take some [very long] time, for any solution.

查看更多
登录 后发表回答