|
1 | 1 | ## PHPermutations |
2 | 2 | [](https://travis-ci.org/drupol/phpermutations) [](https://scrutinizer-ci.com/g/drupol/phpermutations/?branch=master) [](https://scrutinizer-ci.com/g/drupol/phpermutations/?branch=master) [](https://www.versioneye.com/user/projects/5870ade140543803e80abb5b) |
3 | 3 |
|
4 | | -PHP iterators and generators to generate combinations and permutations in an efficient way. |
| 4 | +PHP Iterators and Generators to generate combinations and permutations in an efficient way. |
5 | 5 |
|
6 | 6 | At first the library was created to only generate permutations and combinations. |
| 7 | + |
7 | 8 | In the end, I added other Iterators and Generators like: |
8 | 9 | * Fibonacci numbers, |
9 | 10 | * Perfect numbers, |
10 | 11 | * Prime numbers, |
11 | 12 | * Product of numbers, |
12 | | -* ... more to come. |
| 13 | +* Rotation of an array, |
| 14 | +* Cycling through an array, |
| 15 | +* Permutations, |
| 16 | +* Combinations, |
| 17 | + |
| 18 | +## Introduction |
| 19 | + |
| 20 | +I've always been fascinated by numbers and everything around them... in other words, mathematics. |
| 21 | + |
| 22 | +The library has been written first for being used in [PHPartition](https://github.com/drupol/phpartition), then it has |
| 23 | +been extended here and there. |
| 24 | + |
| 25 | +Its main use is for generating Permutations and Combinations without running out of memory, thanks to |
| 26 | +[PHP Generators](https://secure.php.net/manual/en/language.generators.overview.php) and |
| 27 | +and [Iterators](https://secure.php.net/manual/en/class.iterator.php). |
| 28 | + |
| 29 | +The difference with other combinatorics library is that you can use an extra parameter 'length', that allows you to |
| 30 | +compute Permutations and Combinations of a particular size. |
| 31 | +The other notable difference is that your input arrays may contains any type of object (integers, arrays, strings or |
| 32 | +objects), the library will still continue to work without any trouble. |
| 33 | + |
| 34 | +## Requirements |
| 35 | + |
| 36 | +* PHP >= 5.6, |
| 37 | +* (optional) [PHPUnit](https://phpunit.de/) to run tests. |
| 38 | + |
| 39 | +## How to use |
| 40 | + |
| 41 | +Include this library in your project by doing: |
| 42 | + |
| 43 | +`composer require drupol/phpermutations` |
| 44 | + |
| 45 | +Let's say you want to find all the permutations of the list of number [1, 2, 3, 4, 5] having a length of 3: |
| 46 | + |
| 47 | +```php |
| 48 | +// Create the object |
| 49 | +$permutations = new \drupol\phpermutations\Generators\Permutations([1,2,3,4,5], 3); |
| 50 | + |
| 51 | +// Use a foreach loop. |
| 52 | +foreach ($permutations->generator() as $permutation) {// do stuff} |
| 53 | + |
| 54 | +// Or get the whole array at once. |
| 55 | +$permutations->toArray(); |
| 56 | +``` |
| 57 | + |
| 58 | +Most of the components always has the same arguments except for very few of them. |
| 59 | + |
| 60 | +As the documentation per component is not written yet, I advise you to |
| 61 | +[check the tests](https://github.com/drupol/phpermutations/tree/master/tests/src) to see how to use them. |
| 62 | + |
| 63 | +## Combinations |
| 64 | + |
| 65 | +> In mathematics, a combination is a way of selecting items from a collection, such that (unlike permutations) the order |
| 66 | +> of selection does not matter. |
| 67 | +> -- [_Wikipedia_](https://en.wikipedia.org/wiki/Combination) |
| 68 | +
|
| 69 | +In one sentence: _When the order doesn't matter, it is a Combination._ |
| 70 | + |
| 71 | +## Examples |
| 72 | + |
| 73 | +Let's say we have a group of fruits: |
| 74 | + |
| 75 | +`$list = ['Apple', 'Pear', 'Banana', 'Orange']` |
| 76 | + |
| 77 | +and we want to find the combinations of length: `3`, the result will be: |
| 78 | + |
| 79 | +`['Apple', 'Pear', 'Banana']` |
| 80 | + |
| 81 | +`['Apple', 'Pear', 'Orange']` |
| 82 | + |
| 83 | +`['Apple', 'Banana', 'Orange']` |
| 84 | + |
| 85 | +`['Pear', 'Banana', 'Orange']` |
13 | 86 |
|
14 | 87 | ## Permutations |
15 | 88 |
|
@@ -76,35 +149,16 @@ and we want to find the permutations of length: `3`, the result will be: |
76 | 149 |
|
77 | 150 | `['Orange', 'Banana', 'Pear']` |
78 | 151 |
|
79 | | -## Combinations |
80 | | - |
81 | | -> In mathematics, a combination is a way of selecting items from a collection, such that (unlike permutations) the order |
82 | | -> of selection does not matter. |
83 | | -> -- [_Wikipedia_](https://en.wikipedia.org/wiki/Combination) |
84 | | -
|
85 | | -In one sentence: _When the order doesn't matter, it is a Combination._ |
86 | | - |
87 | | -## Examples |
88 | | - |
89 | | -Let's say we have a group of fruits |
| 152 | +The permutations of length 3 of the array [1, 2, 3, 4, 5] are, _please hold on tight_, the sum of all the permutations |
| 153 | +of each combinations having a length of 3 of that array. |
90 | 154 |
|
91 | | -`$list = ['Apple', 'Pear', 'Banana', 'Orange']` |
| 155 | +## Tests |
92 | 156 |
|
93 | | -and we want to find the combinations of length: `3`, the result will be: |
| 157 | +Each Generators and Iterators are tested using the same values as input. I try to be as much complete as possible with |
| 158 | +the [tests](https://github.com/drupol/phpermutations/tree/master/tests/fixtures). |
| 159 | +Every time the sources are modified, [Travis](https://travis-ci.org/drupol/phpermutations), the continuous integration |
| 160 | +service, tests the code against those tests, this way you are aware if the changes that you are introducing are valid. |
94 | 161 |
|
95 | | -`['Apple', 'Pear', 'Banana']` |
96 | | - |
97 | | -`['Apple', 'Pear', 'Orange']` |
98 | | - |
99 | | -`['Apple', 'Banana', 'Orange']` |
| 162 | +# Contributing |
100 | 163 |
|
101 | | -`['Pear', 'Banana', 'Orange']` |
102 | | - |
103 | | -## Inspired by |
104 | | - |
105 | | -* Many stackoverflow posts. |
106 | | - |
107 | | -## Requirements |
108 | | - |
109 | | -* PHP >= 5.6, |
110 | | -* (optional) [PHPUnit](https://phpunit.de/) to run tests. |
| 164 | +Feel free to contribute to this library by sending Github pull requests. I'm quite reactive :-) |
0 commit comments