Пример #1
0
 /**
  * @en Generating all permutations of a given array
  * @ru Получение всех возможны вариантов перестановок элементов массива
  *
  * $array = array('AAA', 'BBB', 'CCC');
  *
  * $result = arrays::permutation($array); # Array
  *                                        # (
  *                                        #     [0] => Array
  *                                        #     (
  *                                        #         [0] => AAA
  *                                        #         [1] => BBB
  *                                        #         [2] => CCC
  *                                        #     )
  *                                        #
  *                                        #     [1] => Array
  *                                        #     (
  *                                        #         [0] => AAA
  *                                        #         [1] => CCC
  *                                        #         [2] => BBB
  *                                        #     )
  *                                        #
  *                                        #     [2] => Array
  *                                        #     (
  *                                        #         [0] => BBB
  *                                        #         [1] => CCC
  *                                        #         [2] => AAA
  *                                        #     )
  *                                        #
  *                                        #     [3] => Array
  *                                        #     (
  *                                        #         [0] => BBB
  *                                        #         [1] => AAA
  *                                        #         [2] => CCC
  *                                        #     )
  *                                        #
  *                                        #     [4] => Array
  *                                        #     (
  *                                        #         [0] => CCC
  *                                        #         [1] => AAA
  *                                        #         [2] => BBB
  *                                        #     )
  *                                        #
  *                                        #     [5] => Array
  *                                        #     (
  *                                        #         [0] => CCC
  *                                        #         [1] => BBB
  *                                        #         [2] => AAA
  *                                        #     )
  *                                        # )
  *
  * @param array $array
  *
  * @return array
  */
 public static function permutation($array)
 {
     $results = array();
     if (count($array) == 1) {
         $results[] = $array;
     } else {
         for ($i = 0; $i < count($array); $i++) {
             $first = array_shift($array);
             $subresults = items::permutation($array);
             array_push($array, $first);
             foreach ($subresults as $subresult) {
                 $results[] = array_merge(array($first), $subresult);
             }
         }
     }
     return $results;
 }
Пример #2
0
 /**
  * @en Generating all permutations of a given string
  * @ru Строковая комбинаторика. Получение всех возможны вариантов перестановок элементов строки
  *
  *
  * $result = string::permutation('AAA BBB CCC DDD'); # Array
  *                                                   # (
  *                                                   #     [0] => AAA BBB CCC DDD
  *                                                   #     [1] => AAA BBB DDD CCC
  *                                                   #     [2] => AAA CCC DDD BBB
  *                                                   #     [3] => AAA CCC BBB DDD
  *                                                   #     [4] => AAA DDD BBB CCC
  *                                                   #     [5] => AAA DDD CCC BBB
  *                                                   #     [6] => BBB CCC DDD AAA
  *                                                   #     [7] => BBB CCC AAA DDD
  *                                                   #     [8] => BBB DDD AAA CCC
  *                                                   #     [9] => BBB DDD CCC AAA
  *                                                   #     [10] => BBB AAA CCC DDD
  *                                                   #     [11] => BBB AAA DDD CCC
  *                                                   #     [12] => CCC DDD AAA BBB
  *                                                   #     [13] => CCC DDD BBB AAA
  *                                                   #     [14] => CCC AAA BBB DDD
  *                                                   #     [15] => CCC AAA DDD BBB
  *                                                   #     [16] => CCC BBB DDD AAA
  *                                                   #     [17] => CCC BBB AAA DDD
  *                                                   #     [18] => DDD AAA BBB CCC
  *                                                   #     [19] => DDD AAA CCC BBB
  *                                                   #     [20] => DDD BBB CCC AAA
  *                                                   #     [21] => DDD BBB AAA CCC
  *                                                   #     [22] => DDD CCC AAA BBB
  *                                                   #     [23] => DDD CCC BBB AAA
  *                                                   # )
  *
  *
  * @param string $string Any string
  * @param string $delimiter
  *
  * @return array
  */
 public static function permutation($string, $delimiter = ' ')
 {
     $array = items::permutation(explode($delimiter, $string));
     $result = null;
     foreach ($array as $item) {
         $result[] = implode(' ', $item);
     }
     return $result;
 }