From 8fabc1a22f87ec62d4bd9fff2e622018ea76ccb9 Mon Sep 17 00:00:00 2001 From: Iman Abbasi Date: Wed, 8 Feb 2023 19:30:40 +0330 Subject: [PATCH] Combination calculate + Combination Test --- src/Combination.php | 34 +++++++++++-- tests/CombinationTest.php | 102 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 tests/CombinationTest.php diff --git a/src/Combination.php b/src/Combination.php index 50ad430..3d2f491 100644 --- a/src/Combination.php +++ b/src/Combination.php @@ -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()); } /** @@ -99,7 +108,7 @@ public function getPossibilities(): array */ public function countPossibilities(): int { - return count( $this->getPossibilities() ); + return count($this->getPossibilities()); } /** @@ -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; } } \ No newline at end of file diff --git a/tests/CombinationTest.php b/tests/CombinationTest.php new file mode 100644 index 0000000..1c21508 --- /dev/null +++ b/tests/CombinationTest.php @@ -0,0 +1,102 @@ +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()); + } +}