mikegarde/partition

Partition a set of values into equal sets or near equal sets.

0.3.2 2020-03-09 05:01 UTC

This package is auto-updated.

Last update: 2024-11-09 15:31:04 UTC


README

Packagist Packagist GitHub PHP from Travis config GitHub code size in bytes Travis (.org)

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