Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm */
- function permutations($items, $perms = []) {
- if (empty($items)) {
- yield $perms;
- } else {
- for ($i = count($items) - 1; $i >= 0; --$i) {
- $newitems = $items;
- $newperms = $perms;
- list($foo) = array_splice($newitems, $i, 1);
- array_unshift($newperms, $foo);
- yield from permutations($newitems, $newperms);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement