-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower_set.php
More file actions
39 lines (30 loc) · 841 Bytes
/
power_set.php
File metadata and controls
39 lines (30 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
// Author: Marguerite Desko
// Purpose: generate the power sets for incoming lists of stuff
function make_power_set($incoming) {
// base case: return an empty array if the incoming array is empty
if (count($incoming) == 0) {
$retArr[] = array();
return $retArr;
}
else {
// take one item out and put it aside for later
$holdover[] = array_pop($incoming);
// call the make power set function on the rest of the array
$newSet = make_power_set($incoming);
// copy the returned values
$resultSet = $newSet;
// and then go through them, adding the held over item to each array/ result
// set
foreach ($newSet as $new) {
$resultSet[] = array_merge($new, $holdover);
}
// and then return the result set
return $resultSet;
}
}
// TEST
$list = array('1','2','3');
$answer = make_power_set($list);
print_r( $answer);
?>