aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@googlemail.com>2020-04-14 20:22:24 +0200
committerwanderdoc <wanderdoc@googlemail.com>2020-04-14 20:22:24 +0200
commit64329c11e93ede828245a41d57f19893d8d833f8 (patch)
treed4e3cadfdcf72ca9d9b566e646f0d17b81a53237
parentf1e41069c40c9b9821ffc212b465d096da9e0326 (diff)
downloadperlweeklychallenge-club-64329c11e93ede828245a41d57f19893d8d833f8.tar.gz
perlweeklychallenge-club-64329c11e93ede828245a41d57f19893d8d833f8.tar.bz2
perlweeklychallenge-club-64329c11e93ede828245a41d57f19893d8d833f8.zip
Solution to the task # 2.
-rw-r--r--challenge-056/wanderdoc/perl/ch-2.pl70
1 files changed, 70 insertions, 0 deletions
diff --git a/challenge-056/wanderdoc/perl/ch-2.pl b/challenge-056/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..6e6e0726c5
--- /dev/null
+++ b/challenge-056/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+
+
+=prompt
+You are given a binary tree and a sum, write a script to find if the tree has a path such that adding up all the values along the path equals the given sum. Only complete paths (from root to leaf node) may be considered for a sum.
+
+Example:
+
+Given the below binary tree and sum = 22,
+
+ 5
+ / \
+ 4 8
+ / / \
+ 11 13 4
+ / \ \
+ 7 2 1
+
+For the given binary tree, the partial path sum 5 → 4 → 11 = 20 is not valid.
+The script should return the path 5 → 4 → 11 → 2 which sum is 22.
+
+=cut
+
+use Tree::DAG_Node;
+use List::Util qw(sum);
+
+my $SUM = shift || 22;
+
+my $root = Tree::DAG_Node -> new({name => 5});
+my $l_node = $root->new_daughter({name => 4});
+my $r_node = $root->new_daughter({name => 8});
+
+my $n11 = $l_node->new_daughter({name => 11});
+my $n7 = $n11->new_daughter({name => 7});
+my $n2 = $n11->new_daughter({name => 2});
+
+my $n13 = $r_node->new_daughter({name => 13});
+my $n4 = $r_node->new_daughter({name => 4});
+my $n1 = $n4->new_daughter({name => 1});
+
+
+print map("$_\n", @{$root->draw_ascii_tree});
+
+my @paths = $root->leaves_under();
+my @output;
+
+for my $path ( @paths )
+{
+ $path->walk_down({
+ callback => sub {
+ my $node = shift;
+ my @this_sum;
+ unshift @this_sum, $node->name;
+ my @anc = $node->ancestors();
+
+ for my $an ( @anc )
+ {
+ unshift @this_sum, $an->name;
+ }
+ push @output, [@this_sum];
+ },
+ })
+}
+
+# use Data::Dump; dd @output;
+@output = grep $SUM == sum(@$_), @output;
+print join("->", @$_), $/ for @output; \ No newline at end of file