aboutsummaryrefslogtreecommitdiff
path: root/challenge-151/lubos-kolouch/php
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-13 09:53:30 +0000
committerGitHub <noreply@github.com>2022-02-13 09:53:30 +0000
commitc5004cd0b222eca75ed9ffbd8facf4959cedf483 (patch)
tree21ba605f1d22529ca332c68a2b35970afc64db43 /challenge-151/lubos-kolouch/php
parente0d91e53765a48f292162d7d01f3397fe29a06d3 (diff)
parenta107a05db3ee5843c71f0741b4cbb249809285f2 (diff)
downloadperlweeklychallenge-club-c5004cd0b222eca75ed9ffbd8facf4959cedf483.tar.gz
perlweeklychallenge-club-c5004cd0b222eca75ed9ffbd8facf4959cedf483.tar.bz2
perlweeklychallenge-club-c5004cd0b222eca75ed9ffbd8facf4959cedf483.zip
Merge pull request #5643 from LubosKolouch/master
PHP solutions Challenge 151 LK, bug fixes
Diffstat (limited to 'challenge-151/lubos-kolouch/php')
-rw-r--r--challenge-151/lubos-kolouch/php/ch-1.php35
-rw-r--r--challenge-151/lubos-kolouch/php/ch-2.php41
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-151/lubos-kolouch/php/ch-1.php b/challenge-151/lubos-kolouch/php/ch-1.php
new file mode 100644
index 0000000000..70ecb63717
--- /dev/null
+++ b/challenge-151/lubos-kolouch/php/ch-1.php
@@ -0,0 +1,35 @@
+<?php
+
+function get_min_depth($input) {
+
+ #Input: '1 | 2 3 | 4 5'
+
+ # iterate through the layers. If the next layer does not have 2^n items,
+ # there must be a leaf node
+
+ $layers = preg_split("/\|/", $input);
+
+ $layer_count = 1;
+ foreach ($layers as $layer) {
+ # if not defined means we are at the last layer
+
+ try {
+ $items_count = count(preg_split("/\s+/",$layers[$layer_count]));
+ if ($items_count != 2**$layer_count+2) {
+ return $layer_count;
+ }
+
+ $layer_count++;
+
+ } catch (Exception $e) {
+ return $layer_count;
+ }
+
+ }
+}
+
+get_min_depth('1 | 2 3 | 4 5') == 2 or throw new Exception('Test failed');
+get_min_depth('1 | 2 3 | 4 * * 5 | * 6') == 3 or throw new Exception('Test 2 failed');
+
+
+?>
diff --git a/challenge-151/lubos-kolouch/php/ch-2.php b/challenge-151/lubos-kolouch/php/ch-2.php
new file mode 100644
index 0000000000..b31e33d9ad
--- /dev/null
+++ b/challenge-151/lubos-kolouch/php/ch-2.php
@@ -0,0 +1,41 @@
+<?php
+
+$cache = [];
+
+function get_houses_max($houses) {
+
+ global $cache;
+
+ if (in_array(implode(",",$houses), $cache)) {
+ return $cache[implode(",",$houses)];
+ }
+
+ $max_value = 0;
+ $house_index = 0;
+
+ foreach (array_slice($houses, 2) as $house) {
+ $next_houses_values = get_houses_max(array_slice($houses, 2+$house_index));
+ if ($next_houses_values > $max_value) {
+ $max_value = $next_houses_values;
+ }
+
+ $house_index++;
+ }
+
+ $cache[implode(",",$houses)] = $houses[0] + $max_value;
+ return $houses[0] + $max_value;
+
+}
+
+
+if (get_houses_max([2,4,5]) != 7) {
+ throw new Exception('Failed test 1');
+}
+
+$cache = [];
+
+if (get_houses_max([4, 2, 3, 6, 5, 3]) != 13) {
+ throw new Exception('Failed test 2');
+
+}
+?>