Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible word combinations 1

Status
Not open for further replies.

SaRiD

Programmer
Apr 19, 2003
45
A nice tricky one for you all.

I'm trying to figure out all the possible word combinations for a phrase.

E.g. "norman2 stuck on php"

I know the number of combinations as it is a factorial calculation (4 x 3 x 2 x 1)

What I need is to put that into a loop spitting out all the variations.

I've got this so far but it only works up to the total number of words and then goes wacky.

Code:
$kw = "norman2 stuck on php";

$arrKW = split(" ", $kw);

for($i = 0; $i < count($arrKW); $i++) {
	$arrCount[] = $i;
}

$comb = factorial(count($arrKW));

$count = 0;
$swap = 1;
$orgn = 0;
for($a = 0; $a < $comb; $a++) {
	for($i = 0; $i < count($arrKW); $i++) {
		$arrCount[$i]++;
		if($arrCount[$i] >= count($arrKW)) $arrCount[$i] = 0;
	}
	DisplayWords($arrKW, $arrCount);
	$count++;
	if($count >= count($arrKW)) {
		$count = 0;
		$org = $arrCount[$orgn];
		$new = $arrCount[$swap];
		$arrCount[0] = $new;
		$arrCount[$swap] = $org;
		$swap++;
		echo "<br />";
	}
}

function DisplayWords($arrKW, $arrCount) {
	for($i = 0; $i < count($arrKW); $i++) {
		echo $arrCount[$i] . " ";
		#echo $arrKW[$arrCount[$i]] . " ";
	}
	echo "<br />";
}

This works if there are 3 words, but 4 or more and it returns wrong after the number of words - run it to see what I mean.

Any thoughts, or answers?
 
i found this on an old web site in the waybackmachine

slightly adjusted now. it's inefficient and times out on my system (which is slow) but it might give you some ideas

Code:
function pc_next_permutation($p, $size) {
    // slide down the array looking for where we're smaller than the next guy
    for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }

    // if this doesn't occur, we've finished our permutations
    // the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
    if ($i == -1) { return false; }

    // slide down the array looking for a bigger number than what we found before
    for ($j = $size; $p[$j] <= $p[$i]; --$j) { }

    // swap them
    $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;

    // now reverse the elements in between by swapping the ends
    for (++$i, $j = $size; $i < $j; ++$i, --$j) {
         $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
    }

    return $p;
}

$set = split(' ', 'she sells seashells'); // like array('she', 'sells', 'seashells')
$size = count($set) - 1;
$perm = range(0, $size);
$j = 0;

do { 
     foreach ($perm as $i) { $perms[$j][] = $set[$i]; }
} while ($perm = pc_next_permutation($perm, $size) and ++$j);

foreach ($perms as $p) {
    print join(' ', $p) . "\n";
}
 
That works well thank you!

Should save me some time & brain cells

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top