mikegarde / partition
Partition a set of values into equal sets or near equal sets.
Installs: 18 535
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 3
Open Issues: 1
Requires
- php: ^5.6 || ^7.0
- dusank/knapsack: ^10.0.0
Requires (Dev)
- phpunit/phpunit: 5.7.27 - 8 || ^9
README
A PHP function to partition a set of values into equal sets or near equal sets (partitions). Read more about the partition problem on wikipedia.
Installing
Find on packagist.org
composer require mikegarde/partition
Example
<?php require 'vendor/autoload.php'; use partition\partition; $data = [ [ 'id' => 'A', 'value' => 5, ], [ 'id' => 'B', 'value' => 5, ], [ 'id' => 'C', 'value' => 15, ], [ 'id' => 'D', 'value' => 5, ], [ 'id' => 'E', 'value' => 9, ], [ 'id' => 'F', 'value' => 3, ], [ 'id' => 'G', 'value' => 7, ], [ 'id' => 'H', 'value' => 12, ], ]; $partition = new partition($data, 4); $results = $partition->getResults(); $part = $partition->getPartition(0);
A JSON representation of $results
and $part
{ "summary": [ 15, 17, 14, 15 ], "partitions": [ [ { "id": "C", "value": 15 } ], [ { "id": "H", "value": 12 }, { "id": "D", "value": 5 } ], [ { "id": "E", "value": 9 }, { "id": "B", "value": 5 } ], [ { "id": "G", "value": 7 }, { "id": "A", "value": 5 }, { "id": "F", "value": 3 } ] ] }
[ { "id": "C", "value": 15 } ]
Development Notes
docker build . -t php:7.4 docker run --rm -it -v $(pwd):/var/www/html php:7.4 composer install docker run --rm -it -v $(pwd):/var/www/html php:7.4 php ./vendor/bin/phpunit --bootstrap vendor/autoload.php tests