Witam serdecznie. Chciałem stworzyć sobie klasę, która będzie mi generowała losową próbkę spośród zestawu liter.
Następnie dla tak wygenerowanej próbki znajduje wszystkie wariacje (permutacje) bez powtórzeń.
Np dla wylosowanego zbioru licz:
A,B,C
ma zwrócić:
A,B,C
A,C,B
B,A,C
B,C,A
C,A,B
C,B,A
ale dla zbioru: A,B,A
ma już zwrócić
A,A,B
A,B,A
B,A,A
Poniżej znajdziecie klasę, która działa "prawie" dobrze. Niestety jak to bywa, prawie robi różnicę

Zwraca mianowicie dla zestawu A,B,C
A,B,C
B,A,C
C,A,B
czyli zwraca pierwszą znalezioną kombinację, zaczynającą się kolejno od litery:A, następnie od B, a następnie od C
Poniżej klasa i jej wywołanie:
wywołanie<?php
$lvs=new LswVarSet();
$lvs->buildWordSetNoRepeat($lvs->draw(3),3)
?>
klasa<?php
class LswVarSet
{
'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'Ą',
'B', 'B', 'C', 'C', 'C', 'Ć', 'D', 'D', 'D', 'E',
'E', 'E', 'E', 'E', 'E', 'E', 'Ę', 'F', 'G', 'G',
'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I',
'J', 'J', 'K', 'K', 'K', 'L', 'L', 'L', 'Ł', 'Ł',
'M', 'M', 'M', 'N', 'N', 'N', 'N', 'N', 'Ń', 'O',
'O', 'O', 'O', 'O', 'O', 'Ó', 'P', 'P', 'P', 'R',
'R', 'R', 'R', 'S', 'S', 'S', 'S', 'Ś', 'T', 'T',
'T', 'U', 'U', 'W', 'W', 'W', 'W', 'Y', 'Y', 'Y',
'Y', 'Z', 'Z', 'Z', 'Z', 'Z', 'Ź', 'Ż',
);
'A', 'Ą', 'E', 'Ę', 'I', 'O', 'Ó', 'U', 'Y'
);
'B', 'C', 'Ć', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
'Ł', 'M', 'N', 'P', 'R', 'S', 'Ś', 'T', 'W', 'Z',
'Ż', 'Ź'
);
/**
* Check if letter is vowel or not
*
* @return boolean true if is vowel
*/
public function isVowel($letter)
{
return in_array($letter,self::$SCR_VOWEL); }
/**
* Check if letter is consonant or not
*
* @return boolean true if is consonant
*/
public function isConsonant($letter)
{
return in_array($letter,self::$SCR_CONSONANT); }
/**
* Draws from set of letter using {@param $sampleLength} letters
* @return arrat set of letters
*/
public function draw($sampleLength)
{
$draw=array_rand(self::$SCR_LETTER_SET,$sampleLength); foreach ($draw as $key)
$return[]=self::$SCR_LETTER_SET[$key];
return $return;
}
/**
* Build set of letter using (@param $num} letters
* Set of letter is a permutation without repetition
*
* @return list of potential words
*/
public function buildWordSetNoRepeat
(array $set, $num) {
$this->addLetters($num,$set,array(),$result,array()); echo TVarDumper
::dump($result,2
,true); return $result;
}
}
?>