Guest User

Untitled

a guest
Jan 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. <?php
  2.  
  3. function combinations(array $pool, $r) {
  4. $n = count($pool);
  5. if ($r > $n) {
  6. return array();
  7. }
  8. $indices = range(0, $r - 1);
  9. $result = array();
  10. $tuple = array();
  11. foreach ($indices as $i) {
  12. $tuple[] = $pool[$i];
  13. }
  14. $result[] = $tuple;
  15. while (true) {
  16. $dup = true;
  17. for ($i = $r - 1; $i >= 0; --$i) {
  18. if ($indices[$i] != $i + $n - $r) {
  19. $dup = false;
  20. }
  21. }
  22. if ($dup) return $result;
  23. ++$indices[$i];
  24. for ($j = $i + 1; $j < $r; ++$j) {
  25. $indices[$j] = $indices[$j - 1] + 1;
  26. }
  27. $tuple = array();
  28. foreach ($indices as $i) {
  29. $tuple[] = $pool[$i];
  30. }
  31. $result[] = $tuple;
  32. }
  33. }
Add Comment
Please, Sign In to add comment