-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUseCombinationGenerator.php
68 lines (56 loc) · 1.58 KB
/
UseCombinationGenerator.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
66
67
68
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Macocci7\PhpCombination\CombinationGenerator;
// Create an Instance
$c = new CombinationGenerator();
// All Items
$items = [ 'A', 'B', 'C', 'D', 'E', ];
echo sprintf("All Items:\n\t(%s)\n\n", implode(", ", $items));
// Common Format
$fmt = "\t(%s)\n";
// All Combinations
echo "All Combinations:\n";
foreach ($c->all($items) as $e) {
echo sprintf($fmt, implode(', ', $e));
}
echo "\n";
// All Pairs
echo "All Pairs:\n";
foreach ($c->pairs($items) as $e) {
echo sprintf($fmt, implode(', ', $e));
}
echo "\n";
// All Combinations of $n elements
$n = 3;
echo sprintf("All Combinations of %d elements:\n", $n);
foreach ($c->ofN($items, $n) as $e) {
echo sprintf($fmt, implode(', ', $e));
}
echo "\n";
// All Combinations of $a to $b elements
$a = 3;
$b = 4;
echo sprintf(
"All Combinations of %d to %d elements:\n",
$a,
$b,
);
foreach ($c->ofA2B($items, $a, $b) as $e) {
echo sprintf($fmt, implode(', ', $e));
}
echo "\n";
// 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));
}