Re: generate letter combination..

From: Date: Mon, 02 Jul 2007 22:55:31 +0000
Subject: Re: generate letter combination..
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-258062@lists.php.net to get a copy of this message
On 6/26/07, jeffry s <jeffry@bayau.com> 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/

« previous php.general (#258062) next »