Skip to content

Commit

Permalink
Combination calculate + Combination Test
Browse files Browse the repository at this point in the history
  • Loading branch information
Iman Abbasi committed Feb 8, 2023
1 parent 3c86e4f commit 8fabc1a
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/Combination.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,21 @@ public function getItems(): array
return $this->items;
}

/**
* @param $itemIndex
* @return mixed|null
*/
public function getItem($itemIndex): mixed
{
return $this->items[$itemIndex] ?? null;
}

/**
* @return int
*/
public function countItems(): int
{
return count( $this->getItems() );
return count($this->getItems());
}

/**
Expand Down Expand Up @@ -99,7 +108,7 @@ public function getPossibilities(): array
*/
public function countPossibilities(): int
{
return count( $this->getPossibilities() );
return count($this->getPossibilities());
}

/**
Expand All @@ -108,7 +117,26 @@ public function countPossibilities(): int
public function calculate(): static
{
$this->possibilities = [];

if ($this->countItems()) {
$totalPossibilities = pow($this->countItems(), $this->getSelection());
$possibilitiesKeys = [];
for ($possibilityId = 0; $possibilityId < $totalPossibilities; $possibilityId++) {
$itemIndexes = BaseConvert::convert($possibilityId)->to($this->countItems())->setMinDigits($this->getSelection())->calculate()->getResultArray();
if (!$this->canRepetitions() && count($itemIndexes) !== count(array_unique($itemIndexes))) {
continue;
}
$possibility = [];
foreach ($itemIndexes as $itemIndex) {
$possibility[] = $this->getItem($itemIndex);
}
sort($possibility);
$possibilityKey = implode('-', $possibility);
if( !in_array($possibilityKey, $possibilitiesKeys) ){
$possibilitiesKeys[] = $possibilityKey;
$this->possibilities[] = $possibility;
}
}
}
return $this;
}
}
102 changes: 102 additions & 0 deletions tests/CombinationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Graphita\Mathematics\Tests;

use Graphita\Mathematics\Combination;
use PHPUnit\Framework\TestCase;

class CombinationTest extends TestCase
{
public function testGetAndSetRepetitions()
{
$combination = new Combination();

$this->assertFalse($combination->canRepetitions());

$combination->setRepetitions(true);

$this->assertTrue($combination->canRepetitions());
}

public function testGetAndSetItems()
{
$combination = new Combination();

$this->assertIsArray($combination->getItems());
$this->assertEmpty($combination->getItems());
$this->assertEquals(0, $combination->countItems());

$combination->setItems([1, 2, 3]);

$this->assertIsArray($combination->getItems());
$this->assertCount(3, $combination->getItems());
$this->assertEquals(3, $combination->countItems());
}

public function testGetAndSetSelection()
{
$combination = new Combination();

$this->assertEquals(0, $combination->getSelection());

$combination->setSelection(2);

$this->assertEquals(2, $combination->getSelection());
}

public function testGetPossibilitiesWithoutItemsAndSelection()
{
$combination = new Combination();

$this->assertIsArray($combination->getPossibilities());
$this->assertEmpty($combination->getPossibilities());
$this->assertEquals(0, $combination->countPossibilities());

$combination->calculate();

$this->assertIsArray($combination->getPossibilities());
$this->assertEmpty($combination->getPossibilities());
$this->assertEquals(0, $combination->countPossibilities());
}

public function testGetPossibilitiesWithItemsAndWithoutSelection()
{
$combination = new Combination();
$combination->setItems([1, 2, 3]);

$this->assertIsArray($combination->getPossibilities());
$this->assertEmpty($combination->getPossibilities());
$this->assertEquals(0, $combination->countPossibilities());

$combination->calculate();

$this->assertIsArray($combination->getPossibilities());
$this->assertCount(1, $combination->getPossibilities());
$this->assertEquals(1, $combination->countPossibilities());
}

public function testGetPossibilitiesWithItemsAndSelection()
{
$combination = new Combination();
$combination->setItems([1, 2, 3]);
$combination->setSelection(2);
$combination->calculate();

$this->assertIsArray($combination->getPossibilities());
$this->assertCount(3, $combination->getPossibilities());
$this->assertEquals(3, $combination->countPossibilities());
}

public function testGetPossibilitiesWithItemsAndSelectionAndRepetitions()
{
$combination = new Combination();
$combination->setItems([1, 2, 3]);
$combination->setSelection(2);
$combination->setRepetitions(true);
$combination->calculate();

$this->assertIsArray($combination->getPossibilities());
$this->assertCount(6, $combination->getPossibilities());
$this->assertEquals(6, $combination->countPossibilities());
}
}

0 comments on commit 8fabc1a

Please sign in to comment.