diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-01 16:26:46 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-01 16:26:46 +0000 |
| commit | 846bb871fcbbfaf838b301284e1738cccd3dacb6 (patch) | |
| tree | c86b6612c8b996f4476daaa0563f423b75328361 | |
| parent | 46ad741392d4add09d68ca8ab289a93eadcc475e (diff) | |
| parent | 375512909acd0370d179ceb40ee75466f5c49bcc (diff) | |
| download | perlweeklychallenge-club-846bb871fcbbfaf838b301284e1738cccd3dacb6.tar.gz perlweeklychallenge-club-846bb871fcbbfaf838b301284e1738cccd3dacb6.tar.bz2 perlweeklychallenge-club-846bb871fcbbfaf838b301284e1738cccd3dacb6.zip | |
Merge pull request #3122 from akarelas/challenge-093
solutions to #093
| -rwxr-xr-x | challenge-093/alexander-karelas/perl/ch-1.pl | 41 | ||||
| -rwxr-xr-x | challenge-093/alexander-karelas/perl/ch-2.pl | 66 |
2 files changed, 107 insertions, 0 deletions
diff --git a/challenge-093/alexander-karelas/perl/ch-1.pl b/challenge-093/alexander-karelas/perl/ch-1.pl new file mode 100755 index 0000000000..bc01935c9f --- /dev/null +++ b/challenge-093/alexander-karelas/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl +# +# You are given set of co-ordinates @N. Write a script to count maximum points on a straight line when given +# co-ordinates plotted on 2-d plane. + +use v5.30; +use warnings; + +use Test::More; +use List::Util 'max'; + +sub maximum_points { + my @points = @_; + + my %lines = ( + x => {}, + y => {}, + d1 => {}, + d2 => {}, + ); + + foreach my $point (@points) { + $lines{x}{$point->[0]}++; + $lines{y}{$point->[1]}++; + $lines{d1}{$point->[0] - $point->[1]}++; + $lines{d2}{$point->[0] + $point->[1]}++; + } + + my @lines; + foreach my $hashset (values %lines) { + push @lines, values %$hashset; + } + + return max(@lines); +} + +is maximum_points([1,1], [2,2], [3,3]), 3, '1st example'; +is maximum_points([1,1], [2,2], [3,1], [1,3], [5,3]), 3, '2nd example'; +is maximum_points([1,1], [2,1], [3,1], [4,1], [2,2]), 4, 'extra example'; + +done_testing;
\ No newline at end of file diff --git a/challenge-093/alexander-karelas/perl/ch-2.pl b/challenge-093/alexander-karelas/perl/ch-2.pl new file mode 100755 index 0000000000..d16d3c66ec --- /dev/null +++ b/challenge-093/alexander-karelas/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# +# You are given binary tree containing numbers 0-9 only. +# Write a script to sum all possible paths from root to leaf. + +use v5.30; +use warnings; + +use Test::More; + +use List::Util 'sum'; + +sub find_path_sums { + my ($tree, $node_key, $path_sum) = @_; + + $path_sum += $node_key; + my $node = $tree->{$node_key}; + my @path_sums; + if (!defined $node->{left} and !defined $node->{right}) { + @path_sums = ($path_sum); + return \@path_sums; + } + push @path_sums, find_path_sums($tree, $node->{left}, $path_sum)->@* if $node->{left}; + push @path_sums, find_path_sums($tree, $node->{right}, $path_sum)->@* if $node->{right}; + + return \@path_sums; +} + +sub sum_path { + my ($tree) = @_; + + my $root_node = do { + my %values = %$tree; + delete @values{($_->{left} // (), $_->{right} // ())} foreach values %$tree; + (keys %values)[0]; + }; + + my @paths_sums = find_path_sums($tree, $root_node, 0)->@*; + return sum(@paths_sums); +} + + +# example 1 +my %tree = ( + 1 => { left => 2 }, + 2 => { left => 3, right => 4 }, + 3 => {}, + 4 => {}, +); + +is sum_path(\%tree), 13, '1st example'; + + +# example 2 +%tree = ( + 1 => { left => 2, right => 3 }, + 2 => { left => 4 }, + 3 => { left => 5, right => 6 }, + 4 => {}, + 5 => {}, + 6 => {}, +); + +is sum_path(\%tree), 26, '2nd example'; + +done_testing(); |
