Skip to content

Instantly share code, notes, and snippets.

@io-developer
Last active March 4, 2018 16:25
Show Gist options
  • Save io-developer/33f84fb07fcd2356a55f3a9407377022 to your computer and use it in GitHub Desktop.
Save io-developer/33f84fb07fcd2356a55f3a9407377022 to your computer and use it in GitHub Desktop.
binary sets generator
<?php
function generate_sets($values) {
$total = 1 << count($values);
for ($i = 0; $i < $total; $i++) {
$subset = [];
foreach ($values as $j => $val) {
if ($i & (1 << $j)) {
$subset[] = $val;
}
}
yield $subset;
}
}
foreach (generate_sets(range(0, 10)) as $subset) {
echo implode(', ', $subset) . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment