diff options
| author | E. Choroba <choroba@matfyz.cz> | 2020-04-13 13:38:21 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2020-04-13 13:38:21 +0200 |
| commit | 488dbccf2068ee2223e0260b1eef03ba758e94c2 (patch) | |
| tree | 6d97b3fabbf3db0f914feb74bf2e1d7ac800333a | |
| parent | 68bc79d6cd5d1532cc63b3e08d450c6a5cd6c110 (diff) | |
| download | perlweeklychallenge-club-488dbccf2068ee2223e0260b1eef03ba758e94c2.tar.gz perlweeklychallenge-club-488dbccf2068ee2223e0260b1eef03ba758e94c2.tar.bz2 perlweeklychallenge-club-488dbccf2068ee2223e0260b1eef03ba758e94c2.zip | |
Add solutions by E. Choroba to 056 (Diff-K + Path Sum)
| -rwxr-xr-x | challenge-056/e-choroba/perl/ch-1.pl | 14 | ||||
| -rwxr-xr-x | challenge-056/e-choroba/perl/ch-2.pl | 43 |
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-056/e-choroba/perl/ch-1.pl b/challenge-056/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..1f80f2a9f2 --- /dev/null +++ b/challenge-056/e-choroba/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +my @N = (2, 7, 9); +my $k = 2; + +my %in_array; +@in_array{ @N } = 0 .. $#N; + +for (@N) { + say join ', ', @in_array{ $_, $k + $_ } if exists $in_array{ $k + $_ }; +} diff --git a/challenge-056/e-choroba/perl/ch-2.pl b/challenge-056/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..64041211a1 --- /dev/null +++ b/challenge-056/e-choroba/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +use Syntax::Construct qw{ // }; + +sub fill_sum { + _fill_sum($_[0], 0, [], my $path_sums = {}); + return $path_sums +} + +sub _fill_sum { + my ($tree, $parent_sum, $parent_path, $path_sums) = @_; + $tree->{s} = $tree->{v} + ($parent_sum // 0); + $tree->{p} = [ @$parent_path, $tree->{v} ]; + if ($tree->{ch}) { + _fill_sum($_, $tree->{s}, $tree->{p}, $path_sums) for @{ $tree->{ch} }; + } else { + push @{ $path_sums->{ $tree->{s} } }, $tree->{p}; + } +} + +my $tree = { + v => 5, ch => [ + { v => 4, ch => [ + { v => 11, ch => [ + { v => 7 }, + { v => 2 } + ] } + ] }, + { v => 8, ch => [ + { v => 13 }, + { v => 9, ch => [ + { v => 1 } + ] } + ] } + ] }; +my $sum = 22; + + +my $path_sums = fill_sum($tree); +say join '->', @$_ for @{ $path_sums->{$sum} // [] }; |
