Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Permutacja ze zbioru bez powtórzeń, gdzie błąd?
Aztech
post
Post #1





Grupa: Zarejestrowani
Postów: 276
Pomógł: 3
Dołączył: 22.10.2003
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


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ę smile.gif
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
  1. <?php
  2. $lvs=new LswVarSet();
  3. $lvs->buildWordSetNoRepeat($lvs->draw(3),3)
  4. ?>


klasa
  1. <?php
  2. class LswVarSet
  3. {
  4.  
  5. private static $SCR_LETTER_SET = array (
  6. 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'Ą',
  7. 'B', 'B', 'C', 'C', 'C', 'Ć', 'D', 'D', 'D', 'E',
  8. 'E', 'E', 'E', 'E', 'E', 'E', 'Ę', 'F', 'G', 'G',
  9. 'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I',
  10. 'J', 'J', 'K', 'K', 'K', 'L', 'L', 'L', 'Ł', 'Ł',
  11. 'M', 'M', 'M', 'N', 'N', 'N', 'N', 'N', 'Ń', 'O',
  12. 'O', 'O', 'O', 'O', 'O', 'Ó', 'P', 'P', 'P', 'R',
  13. 'R', 'R', 'R', 'S', 'S', 'S', 'S', 'Ś', 'T', 'T', 
  14. 'T', 'U', 'U', 'W', 'W', 'W', 'W', 'Y', 'Y', 'Y',
  15. 'Y', 'Z', 'Z', 'Z', 'Z', 'Z', 'Ź', 'Ż',
  16. );
  17.  
  18. private static $SCR_VOWEL = array (
  19. 'A', 'Ą', 'E', 'Ę', 'I', 'O', 'Ó', 'U', 'Y' 
  20. );
  21.  
  22. private static $SCR_CONSONANT = array (
  23. 'B', 'C', 'Ć', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 
  24. 'Ł', 'M', 'N', 'P', 'R', 'S', 'Ś', 'T', 'W', 'Z',
  25. 'Ż', 'Ź'
  26. );
  27.  
  28. /**
  29.  * Check if letter is vowel or not
  30.  * 
  31.  * @return boolean true if is vowel
  32.  */
  33. public function isVowel($letter)
  34. {
  35. return in_array($letter,self::$SCR_VOWEL);
  36. }
  37.  
  38. /**
  39.  * Check if letter is consonant or not
  40.  * 
  41.  * @return boolean true if is consonant
  42.  */
  43. public function isConsonant($letter)
  44. {
  45. return in_array($letter,self::$SCR_CONSONANT);
  46. }
  47.  
  48. /**
  49.  * Draws from set of letter using {@param $sampleLength} letters
  50.  * @return arrat set of letters 
  51.  */
  52. public function draw($sampleLength)
  53. {
  54. $draw=array_rand(self::$SCR_LETTER_SET,$sampleLength);
  55. foreach ($draw as $key)
  56. $return[]=self::$SCR_LETTER_SET[$key];
  57. return $return;
  58. }
  59.  
  60.  
  61. /**
  62.  * Build set of letter using (@param $num} letters
  63.  * Set of letter is a permutation without repetition
  64.  * 
  65.  * @return list of potential words
  66.  */
  67. public function buildWordSetNoRepeat(array $set, $num)
  68. {
  69. $result=array();
  70. $this->addLetters($num,$set,array(),$result,array());
  71. echo TVarDumper::dump($result,2,true);
  72. return $result;
  73. }
  74. }
  75. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 10:30