tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
59
Task/Power-set/PHP/power-set-1.php
Normal file
59
Task/Power-set/PHP/power-set-1.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
function get_subset($binary, $arr) {
|
||||
// based on true/false values in $binary array, include/exclude
|
||||
// values from $arr
|
||||
$subset = array();
|
||||
foreach (range(0, count($arr)-1) as $i) {
|
||||
if ($binary[$i]) {
|
||||
$subset[] = $arr[count($arr) - $i - 1];
|
||||
}
|
||||
}
|
||||
return $subset;
|
||||
}
|
||||
|
||||
function print_array($arr) {
|
||||
if (count($arr) > 0) {
|
||||
echo join(" ", $arr);
|
||||
} else {
|
||||
echo "(empty)";
|
||||
}
|
||||
echo '<br>';
|
||||
}
|
||||
|
||||
function print_power_sets($arr) {
|
||||
echo "POWER SET of [" . join(", ", $arr) . "]<br>";
|
||||
foreach (power_set($arr) as $subset) {
|
||||
print_array($subset);
|
||||
}
|
||||
}
|
||||
|
||||
function power_set($arr) {
|
||||
$binary = array();
|
||||
foreach (range(1, count($arr)) as $i) {
|
||||
$binary[] = false;
|
||||
}
|
||||
$n = count($arr);
|
||||
$powerset = array();
|
||||
|
||||
while (count($binary) <= count($arr)) {
|
||||
$powerset[] = get_subset($binary, $arr);
|
||||
$i = 0;
|
||||
while (true) {
|
||||
if ($binary[$i]) {
|
||||
$binary[$i] = false;
|
||||
$i += 1;
|
||||
} else {
|
||||
$binary[$i] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$binary[$i] = true;
|
||||
}
|
||||
|
||||
return $powerset;
|
||||
}
|
||||
|
||||
print_power_sets(array());
|
||||
print_power_sets(array('singleton'));
|
||||
print_power_sets(array('dog', 'c', 'b', 'a'));
|
||||
?>
|
||||
21
Task/Power-set/PHP/power-set-2.php
Normal file
21
Task/Power-set/PHP/power-set-2.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
POWER SET of []
|
||||
POWER SET of [singleton]
|
||||
(empty)
|
||||
singleton
|
||||
POWER SET of [dog, c, b, a]
|
||||
(empty)
|
||||
a
|
||||
b
|
||||
a b
|
||||
c
|
||||
a c
|
||||
b c
|
||||
a b c
|
||||
dog
|
||||
a dog
|
||||
b dog
|
||||
a b dog
|
||||
c dog
|
||||
a c dog
|
||||
b c dog
|
||||
a b c dog
|
||||
Loading…
Add table
Add a link
Reference in a new issue