function permutate($prefix, $chars, $permutations) { if (1 == count($chars)) { $permutations[] = $prefix . array_pop($chars); } else { foreach (array_keys($chars) as $index) { $temp = $chars; unset($temp[$index]); $permutations = permutate($prefix . $chars[$index], $temp, $permutations); } } return $permutations; }
function permutate($prefix, $digits, $words) { global $keyMap; if (1 == count($digits)) { foreach (str_split($keyMap[array_pop($digits)]) as $char) { $words[] = $prefix . $char; } } else { foreach (str_split($keyMap[array_shift($digits)]) as $char) { $words = permutate($prefix . $char, $digits, $words); } } return $words; }
function permutate($items, $perms = []) { if (empty($items) === true) { return [$perms]; } $return = []; for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array_unshift($newperms, $foo); $return = array_merge($return, permutate($newitems, $newperms)); } return $return; }
function permutate($items, $permutations = []) { static $allPermutations; if (empty($items)) { $allPermutations[] = $permutations; } else { for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $newPermutations = $permutations; list($nextItem) = array_splice($newitems, $i, 1); array_unshift($newPermutations, $nextItem); permutate($newitems, $newPermutations); } } return $allPermutations; }
protected function findOptimalSeating($people, $stats) { $numOfPeople = count($people); $permutations = permutate($people); $highest = 0; foreach ($permutations as $people) { $happiness = 0; foreach ($people as $key => $person) { if ($key + 1 === $numOfPeople) { $happiness += $stats[$person][$people[0]]; $happiness += $stats[$people[0]][$person]; continue; } $happiness += $stats[$person][$people[$key + 1]]; $happiness += $stats[$people[$key + 1]][$person]; } if ($happiness > $highest) { $highest = $happiness; } } return $highest; }
<div style="font-size:18px; font-weight:bold;margin-bottom: 10px;">Auto Permutation List</div> <div id="save-sentences-message"></div> <input type="button" class="save-auto-sentences" value="Save" style="margin-top:10px; margin-bottom:10px;" /> <?php $no_sentences = $_POST['no_sentences']; $sentences = array(); for ($x = 0; $x < $no_sentences; $x++) { $sentences[$x] = $_POST['sentence' . $x . '_id']; } if ($_POST['use_first']) { $first = $sentences[0]; $sentences = array_slice($sentences, 1); } $limit = 50; permutate($sentences, $limit); ?> <input type="button" class="save-auto-sentences" value="Save" style="margin-top:10px; margin-bottom:10px;" /> <?php function permutate($items, $limit, $perms = array()) { $count = 0; $num_items = count($items); $limit = $limit + $num_items + $num_items - 3; $permutate = function ($items, $perms) use(&$limit, &$count, &$permutate) { // print_pre($count); if (empty($items)) { configure_perm($perms); } else { $count++; for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items;