kieranajp / combinator
Generates combinations of a set.
Installs: 27 106
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^5.4
This package is auto-updated.
Last update: 2024-11-22 21:30:57 UTC
README
Generates combinations of a set
A combination is a set of size k from a number of values > k that does not repeat.
For example, given the values Jim, Jane, Bob, Susan, Ralph
, all the possible combinations of size 3
are as follows:
Jim, Jane, Bob
Jim, Jane, Susan
Jim, Jane, Ralph
Jim, Bob, Susan
Jim, Bob, Ralph
Jim, Susan, Ralph
Jane, Bob, Susan
Jane, Bob, Ralph
Jane, Susan, Ralph
Bob, Susan, Ralph
(credit to this awesome calculator for the example).
Installation
composer require kieranajp/combinator
Usage
The provided Combinator
class is an instance of Iterator
, so combinations can be collected by looping over it.
For example:
use Kieranajp\Combinator\Combinator; $size = 3; $combinator = new Combinator([1, 2, 3, 4, 5], $size); $items = []; foreach ($c as $item) { $items[] = $item; }
The $items
array will now contain all possible combinations of size $size
.
Contributing & Roadmap
Pull requests are welcome. Please adhere to PSR-2. I intend to add a Permutator to this package, as well.
License
MIT License
Copyright (c) 2016 Kieran Patel
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.