diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-02-12 16:32:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-12 16:32:34 +0000 |
| commit | f651ab8add6cf47e6b687eec43adca058d619d1a (patch) | |
| tree | 589f3934516e1f45f3614fec4c6f8d632bb98067 /challenge-151 | |
| parent | dc5f66c3855869d4589862093a02dbfe7465c62c (diff) | |
| parent | 4f2b0c70d8340837bed2c92c5dfbd816d6c612e8 (diff) | |
| download | perlweeklychallenge-club-f651ab8add6cf47e6b687eec43adca058d619d1a.tar.gz perlweeklychallenge-club-f651ab8add6cf47e6b687eec43adca058d619d1a.tar.bz2 perlweeklychallenge-club-f651ab8add6cf47e6b687eec43adca058d619d1a.zip | |
Merge pull request #5642 from LubosKolouch/master
Challenge 151 LK
Diffstat (limited to 'challenge-151')
| -rw-r--r-- | challenge-151/lubos-kolouch/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-151/lubos-kolouch/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-151/lubos-kolouch/python/ch-1.py | 30 | ||||
| -rw-r--r-- | challenge-151/lubos-kolouch/python/ch-2.py | 26 |
4 files changed, 119 insertions, 0 deletions
diff --git a/challenge-151/lubos-kolouch/perl/ch-1.pl b/challenge-151/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..ba464fb82d --- /dev/null +++ b/challenge-151/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,32 @@ +use strict; +use warnings; +use Data::Dumper; + +sub get_min_depth { + my $input = shift; + + #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 =~ 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; + + $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; + diff --git a/challenge-151/lubos-kolouch/perl/ch-2.pl b/challenge-151/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..2f7f7b93f1 --- /dev/null +++ b/challenge-151/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; +use feature qw/say/; + +my %cache; + +sub get_houses_max { + my @houses = @_; + + return $cache{@houses} if $cache{@houses}; + + my $max_value = 0; + my $house_index = 0; + for my $house (@houses[2..@houses-1]) { + my $next_houses_values = get_houses_max(@houses[2+$house_index..@houses-1]); + $max_value = $next_houses_values if $next_houses_values > $max_value; + $house_index++; + } + + $cache{@houses} = $houses[0] + $max_value; + return $houses[0] + $max_value; + +} + +use Test::More; + +is(get_houses_max((2,4,5)), 7); +undef %cache; +is(get_houses_max((4, 2, 3, 6, 5, 3)), 13); + +done_testing; 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 diff --git a/challenge-151/lubos-kolouch/python/ch-2.py b/challenge-151/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..d71588785d --- /dev/null +++ b/challenge-151/lubos-kolouch/python/ch-2.py @@ -0,0 +1,26 @@ +cache = {} + + +def get_houses_max(houses: list): + """Calculate the best way through the houses""" + + try: + return cache[",".join(map(str, houses))] + except KeyError: + pass + + max_value = 0 + house_index = 0 + + for house_index, _ in enumerate(houses[2:]): + next_houses_values = get_houses_max(houses[2 + house_index :]) + if next_houses_values > max_value: + max_value = next_houses_values + + cache[",".join(map(str, houses))] = houses[0] + max_value + return houses[0] + max_value + + +assert get_houses_max([2, 4, 5]) == 7 +cache = {} +assert get_houses_max([4, 2, 3, 6, 5, 3]) == 13 |
