Create a combination of numbers

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

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

 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 cycles and formulas to get the final result, but it does not work. 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 last array with empty values ​​(81 values, in 27 rows of 3 elements), but it contains only 0.

+1
source share
3 answers

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 ())); 
0
source

You indicated that everything will be alright with the pseudo-code .. Sorry, I can’t offer a specific correction for your php code [if these answers appear, they may be more educational], but I chose a recursive solution for this problem.

At each recursion level, try all the possibilities and call the same function to find all combinations of the same 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 

A call with findCombinations(3,3,[],resultList) , where [] is only an empty list, and resultList will contain a list of combinations during the execution of the algorithm. this involution will receive all combinations of size 3 with elements 0,1,2.

A note about complexity: The number of possibilities grows exponentially [O (range of size )], so if you try to call it using 20.20, for example, it may take some time for any solution.

+2
source
 $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 all numbers from 0 to X with the base $nbNumeric , so we simply convert the maximum number to base 10, iterate over it using the usual 10 operational operators and convert them back to the base $nbNumeric again.

Probably more readable, but actually 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; } 
+2
source

Source: https://habr.com/ru/post/910336/


All Articles