Recreation of all reset



  • There is a set of elements, and all possible options for streamlining the mass should be identified.

    Massive looks like this.

    $arr = explode(' ', 'she sells seashells');
    

    I want the next result.

    she sells seashells
    sells she seashells
    she seashells sells
    seashells she sells
    sells seashells she
    seashells sells she
    

    I'm a newcomer in this case, so I'm happy to have any help. Thank you.



  • Use one of the two conversion algorithms discussed further.

    Discussion

    The function pc_permute() given in example 1 is the PHP modification of the basic recurring function.

    example 1

    function pc_permute($items, $perms = array()) {
        if (empty($items)) {
            echo implode(' ', $perms) . "<br>";
        } else {
            for ($i = count($items) -1; $i >= 0; --$i) {
                $newitems = $items;
                $newperms = $perms;
                list($foo) = array_splice($newitems, $i, 1);
                array_unshift($newperms, $foo);
                pc_permute($newitems, $newperms);
            }
        }
    }
    pc_permute(explode(' ', 'she sells seashells'));
    

    This is an elegant, but not effective, as it does Copies everywhere. Moreover, it is not easy to modify this function, that it returns to the seal instead of the printing without the nacopal I'd like to see them in the global variable.

    example 2

    function pc_next_permutation($p, $size) {
        // проходим массив сверху вниз в поисках числа, которое меньше следующего
        for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }
        // если такого нет, прекращаем перестановки
        // массив перевернут: (1, 2, 3, 4) => (4, 3, 2, 1)
        if ($i == -1) { return false; }
        // проходим массив сверху вниз в поисках числа,
        // превосходящего найденное ранее
        for ($j = $size; $p[$j] <= $p[$i]; --$j) { }
        // переставляем их
        $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
        // теперь переворачиваем массив путем перестановки элементов,
        // начиная с конца
        for (++$i, $j = $size; $i < $j; ++$i, --$j) {
            $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
        }
    
    return $p;
    

    }
    $set = explode(' ', 'she sells seashells');
    $size = count($set) -1;
    $perm = range(0, $size);
    $j = 0;
    do {
    foreach ($perm as $i) { $perms[$j][] = $set[$i]; }
    } while ($perm = pc_next_permutation($perm, $size) and ++$j);
    foreach ($perms as $p) {
    echo implode(' ', $p) . "<br>";
    }

    The idea of Dominus is that instead of manipulation with the body itself, it is possible to revert whole numbers. Then, in order to get the real change, we'll be back in line with the numbers of the elements of the body, the original idea.
    However, this arrangement has some shortcomings. For us PHP programmers, frequent retrievals, retrievals and
    clusters, i.e. that Perl is central.
    The process of computing whole numbers goes through
    A series of steps to be taken for each reshuffle; and since it does not resemble previous reversals, each time
    starting with the initial reset. Why does it work re-performing if we can help him?




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2