prepare for more algorithms

This commit is contained in:
Dennis Eichhorn 2019-09-06 21:25:09 +02:00
parent 2c4155b674
commit 27035d8be7
11 changed files with 222 additions and 3 deletions

View File

@ -41,10 +41,10 @@ final class AccountManager implements \Countable
/**
* Session.
*
* @var null|SessionInterface
* @var SessionInterface
* @since 1.0.0
*/
private ?SessionInterface $session = null;
private SessionInterface $session;
/**
* Constructor.

View File

@ -0,0 +1,76 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\CoinMatching
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\CoinMatching;
/**
* Matching a value with a set of coins
*
* @package phpOMS\Algorithm\CoinMatching
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class MinimumCoinProblem
{
private function __construct()
{
}
/**
* Find the minimum amount of coins that are required to match a value
*
* @param array $coins Types of coins available (every coin has infinite availablity)
* @param int $value Value to match with the coins
*
* @return array
*
* @since 1.0.0
*/
public static function getMinimumCoinsForValueI(array $coins, int $value) : array
{
// amount of required coins for different values
$table = [0];
$usedCoins = [];
for ($i = 1; $i <= $value; ++$i) {
$table[$i] = \PHP_INT_MAX;
}
$m = \count($coins);
for ($i = 1; $i <= $value; ++$i) {
for ($j = 0; $j < $m; ++$j) {
if ($coins[$j] <= $i) {
if ($coins[$j] === null) {
continue;
}
$subRes = $table[$i - $coins[$j]];
if ($subRes !== \PHP_INT_MAX
&& $subRes + 1 < $table[$i]
) {
$table[$i] = $subRes + 1;
$usedCoins[$i] = \array_merge($usedCoins[$i - $coins[$j]], [$coins[$j]]);
$coins[$j] = null;
}
}
}
}
return $usedCoins[$value];
}
}

View File

View File

View File

@ -0,0 +1,63 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\Knappsack
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Backpack;
/**
* Matching a value with a set of coins
*
* @package phpOMS\Algorithm\Knappsack
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Backpack
{
private $maxCost = 0.0;
private $value = 0.0;
private $cost = 0.0;
private array $items = [];
public function __construct($maxCost)
{
$this->maxCost = $maxCost;
}
public function getValue()
{
return $this->value;
}
public function getCost()
{
return $this->cost;
}
public function getItems() : array
{
return $this->items;
}
public function addItem(Item $item) : void
{
$this->items[] = $item;
$this->value += $item->getValue();
$this->cost += $item->getCost();
}
}

View File

View File

View File

@ -0,0 +1,40 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\Knappsack
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Knappsack;
/**
* Matching a value with a set of coins
*
* @package phpOMS\Algorithm\Knappsack
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Item
{
private $value = 0.0;
private $cost = 0.0;
public function getValue()
{
return $this->value;
}
public function getCost()
{
return $this->cost;
}
}

View File

View File

@ -0,0 +1,40 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\CoinMatching;
use phpOMS\Algorithm\CoinMatching\MinimumCoinProblem;
require_once __DIR__ . '/../../Autoloader.php';
/**
* @testdox phpOMS\Algorithm\CoinMatching\MinimumCoinProblem: Test coin matching a value problem
*
* @internal
*/
class MinimumCoinProblemTest extends \PHPUnit\Framework\TestCase
{
public function testMinimumCoins() : void
{
self::assertEquals(
[9, 6, 5, 1],
MinimumCoinProblem::getMinimumCoinsForValueI([6, 6, 5], 17)
);
self::assertEquals(
[9, 6, 5, 6, 1],
MinimumCoinProblem::getMinimumCoinsForValueI([6, 6, 5], 17)
);
}
}

View File

@ -12,7 +12,7 @@
*/
declare(strict_types=1);
namespace phpOMS\tests\Algorithm\Sort;
namespace phpOMS\tests\Algorithm\PathFinding;
use phpOMS\Algorithm\PathFinding\Grid;
use phpOMS\Algorithm\PathFinding\MovementType;