Advertisement
Guest User

Untitled

a guest
Mar 6th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.46 KB | None | 0 0
  1. /** http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm */
  2. function permutations($items, $perms = []) {
  3.     if (empty($items)) {
  4.         yield $perms;
  5.     } else {
  6.         for ($i = count($items) - 1; $i >= 0; --$i) {
  7.             $newitems = $items;
  8.             $newperms = $perms;
  9.             list($foo) = array_splice($newitems, $i, 1);
  10.             array_unshift($newperms, $foo);
  11.             yield from permutations($newitems, $newperms);
  12.         }
  13.     }
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement