aboutsummaryrefslogtreecommitdiff
path: root/challenge-151
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2022-02-12 20:46:07 +0100
committerLubos Kolouch <lubos@kolouch.net>2022-02-12 20:46:07 +0100
commit78574c7cfa86b7040aa38435b41ab56357c4bc86 (patch)
treeafe5189a0f3a26f8ca6bd3a63907fbd81069cad5 /challenge-151
parente0d91e53765a48f292162d7d01f3397fe29a06d3 (diff)
downloadperlweeklychallenge-club-78574c7cfa86b7040aa38435b41ab56357c4bc86.tar.gz
perlweeklychallenge-club-78574c7cfa86b7040aa38435b41ab56357c4bc86.tar.bz2
perlweeklychallenge-club-78574c7cfa86b7040aa38435b41ab56357c4bc86.zip
Challenge 151 LK Task 1 PHP
Diffstat (limited to 'challenge-151')
-rw-r--r--challenge-151/lubos-kolouch/php/ch-1.php33
1 files changed, 33 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..3f70cb20c8
--- /dev/null
+++ b/challenge-151/lubos-kolouch/php/ch-1.php
@@ -0,0 +1,33 @@
+<?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');
+