aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2022-02-12 13:43:22 +0100
committerLubos Kolouch <lubos@kolouch.net>2022-02-12 13:43:22 +0100
commit00bcd189253a7cf50460828f2693bdeba4e42bef (patch)
tree2acf5ee5b6c3a4f76f8f3fceae5f7f4de35f785d
parent64d436c521f36b32002d57b74d45b044f7773fd2 (diff)
downloadperlweeklychallenge-club-00bcd189253a7cf50460828f2693bdeba4e42bef.tar.gz
perlweeklychallenge-club-00bcd189253a7cf50460828f2693bdeba4e42bef.tar.bz2
perlweeklychallenge-club-00bcd189253a7cf50460828f2693bdeba4e42bef.zip
Challenge 151 LK Task 1 Python
-rw-r--r--challenge-151/lubos-kolouch/python/ch-1.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-151/lubos-kolouch/python/ch-1.py b/challenge-151/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..fd34a05784
--- /dev/null
+++ b/challenge-151/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,30 @@
+import re
+
+
+def get_min_depth(input: str):
+ """Calculate the depth"""
+ # 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
+
+ input = re.sub(r"\s", "", input)
+ layers = input.split("|")
+
+ for layer_count, _ in enumerate(layers, 1):
+ # if not defined means we are at the last layer
+
+ try:
+ items_count = len(layers[layer_count])
+ except IndexError:
+ return layer_count
+
+ if items_count != 2**layer_count:
+ return layer_count
+
+ return None
+
+
+assert get_min_depth("1 | 2 3 | 4 5") == 2
+assert get_min_depth("1 | 2 3 | 4 * * 5 | * 6") == 3
+assert get_min_depth("1 | 2 3") == 2