Last active
March 4, 2018 16:25
-
-
Save io-developer/97c20a1dc0b73a2c6d44fb2dd0bf3938 to your computer and use it in GitHub Desktop.
binary sets
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 sets_of($list) { | |
$sets = []; | |
$total = 1 << count($list); | |
for ($i = 0; $i < $total; $i++) { | |
$sub = []; | |
foreach ($list as $j => $val) { | |
if ($i & (1 << $j)) { | |
$sub[] = $val; | |
} | |
} | |
$sets[] = $sub; | |
} | |
return $sets; | |
} | |
print_r(sets_of([1, 2, 3, 4])); | |
print_r(sets_of(["a", "b", "c", "d"])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment