Skip to content

Instantly share code, notes, and snippets.

@io-developer
Last active March 4, 2018 16:25
Show Gist options
  • Save io-developer/97c20a1dc0b73a2c6d44fb2dd0bf3938 to your computer and use it in GitHub Desktop.
Save io-developer/97c20a1dc0b73a2c6d44fb2dd0bf3938 to your computer and use it in GitHub Desktop.
binary sets
<?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