-
Notifications
You must be signed in to change notification settings - Fork 1
/
UseCombination.php
65 lines (53 loc) · 1.56 KB
/
UseCombination.php
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Macocci7\PhpCombination\Combination;
// Create an Instance
$c = new Combination();
// All Items
$items = [ 'A', 'B', 'C', 'D', 'E', ];
echo sprintf("All Items:\n\t(%s)\n\n", implode(", ", $items));
// Call back
$f = function (array $array): string {
return sprintf("(%s)", implode(', ', $array));
};
// All Combinations
echo sprintf(
"All Combinations:\n\t%s\n\n",
implode("\n\t", array_map($f, $c->all($items)))
);
// All Pairs
echo sprintf(
"All Pairs:\n\t%s\n\n",
implode("\n\t", array_map($f, $c->pairs($items)))
);
// All Combinations of $n elements
$n = 3;
echo sprintf(
"All Combinations of %d elements:\n\t%s\n\n",
$n,
implode("\n\t", array_map($f, $c->ofN($items, $n)))
);
// All Combinations of $a to $b elements
$a = 3;
$b = 4;
echo sprintf(
"All Combinations of %d to %d elements:\n\t%s\n\n",
$a,
$b,
implode("\n\t", array_map($f, $c->ofA2B($items, $a, $b)))
);
// All Combinations of $a1, $a2 and $a3
$a1 = [ 'A1', 'A2', ];
$a2 = [ 'B1', 'B2', 'B3', ];
$a3 = [ 'C1', 'C2', 'C3', 'C4', ];
echo "All Combinations of multiple arrays:\n";
echo sprintf("\tArray1: (%s)\n", implode(', ', $a1));
echo sprintf("\tArray2: (%s)\n", implode(', ', $a2));
echo sprintf("\tArray3: (%s)\n", implode(', ', $a3));
$r = $c->fromArrays([$a1, $a2, $a3, ]);
$count = count($a1) * count($a2) * count($a3);
$n = strlen((string) $count);
echo sprintf("\tThere're %d patterns:\n", $count);
foreach ($r as $i => $e) {
echo sprintf("\t%" . $n . "d: (%s)\n", $i + 1, implode(', ', $e));
}