From 78574c7cfa86b7040aa38435b41ab56357c4bc86 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 12 Feb 2022 20:46:07 +0100 Subject: Challenge 151 LK Task 1 PHP --- challenge-151/lubos-kolouch/php/ch-1.php | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-151/lubos-kolouch/php/ch-1.php (limited to 'challenge-151') 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 @@ + Date: Sat, 12 Feb 2022 20:58:15 +0100 Subject: Fix a failed count for numbers with more than 1 digit --- challenge-151/lubos-kolouch/perl/ch-1.pl | 16 ++++++++-------- challenge-151/lubos-kolouch/python/ch-1.py | 13 ++++++++----- 2 files changed, 16 insertions(+), 13 deletions(-) (limited to 'challenge-151') diff --git a/challenge-151/lubos-kolouch/perl/ch-1.pl b/challenge-151/lubos-kolouch/perl/ch-1.pl index ba464fb82d..c60f681143 100644 --- a/challenge-151/lubos-kolouch/perl/ch-1.pl +++ b/challenge-151/lubos-kolouch/perl/ch-1.pl @@ -1,6 +1,5 @@ use strict; use warnings; -use Data::Dumper; sub get_min_depth { my $input = shift; @@ -9,24 +8,25 @@ sub get_min_depth { # iterate through the layers. If the next layer does not have 2^n items, # there must be a leaf node - - $input =~ s/\s//g; + my @layers = split /\|/, $input; my $layer_count = 1; for my $layer (@layers) { + # if not defined means we are at the last layer return $layer_count unless defined $layers[$layer_count]; - my $items_count = length($layers[$layer_count]); - return $layer_count unless $items_count == 2**$layer_count; + my $items_count = scalar( split /\s+/, $layers[$layer_count] ); + return $layer_count unless $items_count == 2**$layer_count + 1; $layer_count++; } } use Test::More; -is(get_min_depth('1 | 2 3 | 4 5'), 2); -is(get_min_depth('1 | 2 3 | 4 * * 5 | * 6'), 3); -done_testing; +is( get_min_depth('1 | 2 3 | 4 5'), 2 ); +is( get_min_depth('1 | 2 3 | 4 * * 5 | * 6'), 3 ); +is( get_min_depth('1 | 20 3 | 4 * * 5 | * 6'), 3 ); +done_testing; diff --git a/challenge-151/lubos-kolouch/python/ch-1.py b/challenge-151/lubos-kolouch/python/ch-1.py index fd34a05784..b30797ece3 100644 --- a/challenge-151/lubos-kolouch/python/ch-1.py +++ b/challenge-151/lubos-kolouch/python/ch-1.py @@ -1,21 +1,22 @@ import re -def get_min_depth(input: str): +def get_min_depth(my_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("|") + layers = my_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]) + layers[layer_count] = re.sub(r"^\s+", "", layers[layer_count]) + layers[layer_count] = re.sub(r"\s+$", "", layers[layer_count]) + + items_count = len(re.split(r"\s+", layers[layer_count])) except IndexError: return layer_count @@ -27,4 +28,6 @@ def get_min_depth(input: str): 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 | 20 3 | 4 * * 5 | * 6") == 3 + assert get_min_depth("1 | 2 3") == 2 -- cgit From 47782debf427a239672b64a81cddbe0e3eb9935a Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 12 Feb 2022 21:01:46 +0100 Subject: Close the PHP tag in ch-1 --- challenge-151/lubos-kolouch/php/ch-1.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'challenge-151') diff --git a/challenge-151/lubos-kolouch/php/ch-1.php b/challenge-151/lubos-kolouch/php/ch-1.php index 3f70cb20c8..70ecb63717 100644 --- a/challenge-151/lubos-kolouch/php/ch-1.php +++ b/challenge-151/lubos-kolouch/php/ch-1.php @@ -31,3 +31,5 @@ function get_min_depth($input) { 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'); + +?> -- cgit From a107a05db3ee5843c71f0741b4cbb249809285f2 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 12 Feb 2022 21:16:34 +0100 Subject: Challenge 151 Task 2 LK PHP --- challenge-151/lubos-kolouch/php/ch-2.php | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-151/lubos-kolouch/php/ch-2.php (limited to 'challenge-151') 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 @@ + $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'); + +} +?> -- cgit