Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Confusing Permutations T_T

heyotetsuoheyotetsuo Posts: 1Member
problem: trying to generate all possible permutations from an array in all possible sampling amounts (truncated output example below)


generating using 3objects of 6set with values(0,1,2,3,4,5)..
012 013 014 015
021 023 024 025
031 032 034 035
041 042 043 045
051 052 053 054
102 103 104 105

etc.. until we get to..

541 542 543 545
FINISHED.



it seems like it's a lot easier to program if you have a limitation on the number of values in the array, but for my purposes it really needs to be flexible.

after a few weeks of being stumped, the most promising approach i've had is a slot machine. So i made a slot class..



$userdat = $_GET["dat"];
$tray = str_split($userdat); //this will be like a card deck
class slot
{
public $val = 0;
function write(){ //using $val to retrieve $tray values
global $tray;
echo $tray[$this->val];
}
function roll(){//increments to the next value in the $tray
global $traylen;
if ($this->val == $traylen - 1){ //controls max value
$this->val = 0;
} else {
$this->val++;
}
}
}



..so i made an array of slot()'s and called it $slotmac...


$slotmac = array();
for ($i=0; $i<count($tray); $i++){
array_push($slotmac, new slot());
}


..so with the stage set - what would be an elegant approach to getting each slot to be aware of repetitions and increment intelligently? i've already set up factorial() and permutation() below..


function fact($n){
if ($n == 0) return 1;
$r = $n;
while ($n>1){
$r *= (--$n);
}
return $r;
}
function perm($k){
global $traylen;
$n = $traylen;
if ($n == $k) return fact($k);
return fact($n)/fact($n - $k);
}
Sign In or Register to comment.