Re: generate letter combination..

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 6/26/07, jeffry s <jeffry@xxxxxxxxx> wrote:
i made a mistake when i say i can solve this problem to a friend.
finally i realize it is not as simple as i appear to be.

what he ask me to do is write a simple script that, given a word.. the
script will generate a list of words combinaton
from the each character in in that word. i am tired thinking.

to make it clear. i give a simple scenario. i don't have an idea what i
should name my baby.
so, i took my wife  name and me and input to the script.(i want a name that
is from our name combination).

later the output will present many words that i can choose.

the input string can be of any length.

anyone have an idea?

It's a called a 'permutation'.  The example in the second comment
seems to meet your requirements:

http://php.net/shuffle


function fact( $int )
{
  if( $int < 2 ) return 1;
  for( $f = 2; $int - 1 > 1; $f *= $int-- );
  return $f;
}

function del( $s, $n )
{
   return substr( $s, 0, $n ) . substr( $s, $n + 1 );
}

function perm( $s, $n = null )
{
   if( $n === null ) return perms( $s );

   $r = '';

   $l = strlen( $s );

   while( $l-- )
   {
       $f = fact( $l );
       $p = floor( $n / $f );
       $r .= $s{$p};
       $s = del( $s, $p );
       $n-= $p * $f;
   }

   $r .= $s;

   return $r;
}

function perms( $s )
{
   $p = array();
   for( $i = 0; $i < fact( strlen( $s ) ); $i++ )
       $p[] = perm( $s, $i );
   return $p;
}

$s = 'foobar';

$strings = perms( $s );

foreach( $strings as $s )
{
   echo "$s\n";
}


--
Greg Donald
http://destiney.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux