skal alle være unike? I så fall blir det 6 mulige, 3! = 6
uansett, søk på "shuffle + word" på Google. har du tilgang til PHP, e.l. kan du gjøre slikt: (ikke testet)
Kode
<?php
function shuffle_word ($input) {
$bits = str_split ( $input );
$size = sizeof ( $bits );
$combinations = array_product ( range ( 1, $size ) );
$out = array ();
for ($Run = 0; $Run < $combinations; ++$Run) {
$word = '';
shuffle ($bits);
for ($x = 0; $x < $size; ++$x) {
$word .= $bits[$x];
}
$out[] = $word;
}
return array_unique ($out);
}
print_r ( shuffle_word ('Hallo' ));
?>
Edit: så nå du ønsker kun tre bokstaver. Kan du PHP er det jo fort unnagjort, but, but.
Testet koden, denne fungerer mye bedre:
Kode
<?php
function shuffle_word ($input) {
$size = strlen ( $input );
$combinations = array_product ( range ( 1, $size ) );
$out = array ();
for ($Run = 0; $Run < $combinations; ++$Run) {
$word = str_shuffle ($input);
// Duplicate found. This mean it did not shuffle good enough, we "try again" by pretending the shuffle did not count thus subtracting the try from Run
if (in_array ($word, $out)) {
--$Run;
continue;
}
$out[] = $word;
}
return $out;
}
echo '<pre>', print_r ( shuffle_word ('abc'), true), '</pre>';
?>
Sist endret av davste; 9. mars 2009 kl. 20:16.
Grunn: Automatisk sammenslåing med etterfølgende innlegg.