Last active
March 4, 2018 16:25
-
-
Save io-developer/33f84fb07fcd2356a55f3a9407377022 to your computer and use it in GitHub Desktop.
binary sets generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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