From 2bf2ab5062a29a4f9232683b39f2a4dee4f6ad8d Mon Sep 17 00:00:00 2001 From: Yuriy Marad Date: Fri, 28 Dec 2018 18:24:40 +0200 Subject: [PATCH] Add PHP example based on DS extension for chapter 08 - greedy algorithms (#97) * Add PHP example based on DS extension for chapter 08 - greedy algorithms * Add code formatting * Add code formatting / 2 --- 08_greedy_algorithms/php/01_set_covering.php | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 08_greedy_algorithms/php/01_set_covering.php diff --git a/08_greedy_algorithms/php/01_set_covering.php b/08_greedy_algorithms/php/01_set_covering.php new file mode 100644 index 0000000..499fe88 --- /dev/null +++ b/08_greedy_algorithms/php/01_set_covering.php @@ -0,0 +1,36 @@ +isEmpty()) { + $bestStation = null; + $statesCovered = new Set(); + foreach (array_keys($stations) as $station) { + $states = $stations[$station]; + $covered = new Set($statesNeeded); + $covered = $covered->filter(function ($value) use ($states) { + return $states->contains($value); + }); + if ($covered->count() > $statesCovered->count()) { + $bestStation = $station; + $statesCovered = $covered; + } + } + $statesNeeded = new Set($statesNeeded); + $statesNeeded = $statesNeeded->filter(function ($value) use ($statesCovered) { + return !$statesCovered->contains($value); + }); + $finalStations->add($bestStation); +} + +print_r($finalStations); // ["kone", "ktwo", "kthree", "kfive"] \ No newline at end of file