Word Permutations

I recently had a programming challenge where I needed to explicitly list all possible permutations of a word. The next phase was going to be checking those words against a standard dictionary to see if each/any was a "dictionary word" and thus qualified/disqualified from the list. That part would be easy to implement using the below block, so here's the Phase I version of the code.

function array_2D_permutate($items, $perms = array( ))
{
 static $permutated_array;
 if (empty($items))
 {
  $permutated_array[]=$perms;
  #print_r($new);
  #print join(' ', $perms) . "\n";
 } else {
  for ($i = count($items) - 1; $i >= 0; --$i)
  {
   $newitems = $items;
   $newperms = $perms;
   list($foo) = array_splice($newitems, $i, 1);
   array_unshift($newperms, $foo);
   array_2D_permutate($newitems, $newperms);
  }
  return $permutated_array;
 }
} 

$word='jimbob';

$len=strlen($word);
for($i=1;$i

$result=array_2D_permutate($arr);
foreach($result as $key=>$value)
{
 unset($theword);
 foreach($value as $key2=>$value2)
 {
  $theword.=$value2;
 }
 $words[]=$theword;
}
$unique_words=array_unique($words);
foreach($unique_words as $key=>$value)
{
 echo $value."
\n"; }

0 comments on Word Permutations