PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1845
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Confusing Permutations T_T Posted by heyotetsuo on 17 Oct 2012 at 4:07 PM
problem: trying to generate all possible permutations from an array in all possible sampling amounts (truncated output example below)

<output>
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
</output>
etc.. until we get to..
<output>
541 542 543 545
FINISHED.
</output>


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..


<code>
$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++;
}
}
}
</code>


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

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

..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..

<code>
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);
}
</code>



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.