/** * @depends testWillOverload */ public function testGenerateAllCombinationsMaxVM() { $placements = array('a' => array('A:2', 'A:3'), 'b' => array('B:1', 'B:2'), 'c' => array('C:2', 'C:3'), 'd' => array('D:1', 'D:2', 'D:3')); //$filtered = Combinations::generateAllCombinationsMaxVM(array_values($scenario['placements']),2); $filtered = Combinations::generateAllCombinationsMaxVM($placements, 2); $this->assertEquals(16, count($filtered)); $filtered = Combinations::generateAllCombinationsMaxVM($placements, 3); $this->assertEquals(23, count($filtered)); }
static function filterCombinationsByMaxVm($combinations, $max) { foreach ($combinations as $key => $combination) { $p = Combinations::montaVeP(array($combination))[1]; if (Combinations::hasMoreThen($p, $max)) { unset($combinations[$key]); } } return $combinations; }
public function megaCookieWithCalories($teaspoons, $calories) { $megaCookie = null; $ingredientNames = $this->larder->ingredients(); foreach ($this->combinations->generate($teaspoons) as $combination) { $quantities = []; foreach ($combination as $k => $amount) { $ingredient = $ingredientNames[$k]; $quantities[$ingredient] = $amount; } $recipe = new Recipe($this->larder, $teaspoons, $quantities); if ($recipe->totalScoreForProperty("calories") != $calories) { continue; } if (!$megaCookie || $recipe->totalScoreForRecipe() > $megaCookie->totalScoreForRecipe()) { $megaCookie = $recipe; } } return $megaCookie; }
<?php Combinations::main(); class Combinations { private static $k = 3; private static $n = 5; private static $arr; public static function main() { self::comb(0, 0); } private static function comb($index, $start) { if ($index >= self::$k) { self::printArr(); } else { for ($i = $start; $i < self::$n; $i++) { self::$arr[$index] = $i; self::comb($index + 1, $i); } } } private static function printArr() { echo implode(", ", self::$arr); echo "<br />"; } }