aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-056/alicia-bielsa/perl/ch-1.pl32
-rw-r--r--challenge-056/alicia-bielsa/perl/ch-2.pl73
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-056/alicia-bielsa/perl/ch-1.pl b/challenge-056/alicia-bielsa/perl/ch-1.pl
new file mode 100644
index 0000000000..1832bf4df2
--- /dev/null
+++ b/challenge-056/alicia-bielsa/perl/ch-1.pl
@@ -0,0 +1,32 @@
+use strict;
+use warnings;
+use Data::Dumper;
+
+#Diff-K
+
+#You are given an array @N of positive integers (sorted) and another non negative integer k.
+
+#Write a script to find if there exists 2 indices i and j such that A[i] - A[j] = k and i != j.
+
+#It should print the pairs of indices, if any such pairs exist.
+
+#Example:
+
+# @N = (2, 7, 9)
+# $k = 2
+
+#Output : 2,1
+
+
+my @N = (2, 7, 9);
+my $k = 2;
+
+
+foreach my $i (reverse(1..$#N)){
+ foreach my $j (reverse(0..$i-1)){
+ my $difference = $N[$i] - $N[$j];
+ if ($difference == $k){
+ print "Output : $i,$j\n";
+ }
+ }
+} \ No newline at end of file
diff --git a/challenge-056/alicia-bielsa/perl/ch-2.pl b/challenge-056/alicia-bielsa/perl/ch-2.pl
new file mode 100644
index 0000000000..c92c5f94d2
--- /dev/null
+++ b/challenge-056/alicia-bielsa/perl/ch-2.pl
@@ -0,0 +1,73 @@
+use strict;
+use warnings;
+use Data::Dumper;
+
+#Path Sum
+
+#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 9
+# / \ \
+# 7 2 1
+
+#For the given binary tree, the partial path sum 5 → 8 → 9 = 22 is not valid.
+
+#The script should return the path 5 → 4 → 11 → 2 whose sum is 22.
+
+my $sum = 22;
+my $binaryTree = buildBinaryTree();
+my @aNodes = ();
+findCompletePaths(\@aNodes, $binaryTree->{root});
+
+sub findCompletePaths {
+ my $refANodes = shift;
+ my $node = shift;
+ my @aPath =(@{$refANodes},$node->{value} );
+ if (defined($node->{left})){
+ findCompletePaths(\@aPath,$node->{left});
+ }
+ if (defined($node->{right})){
+ findCompletePaths(\@aPath,$node->{right});
+ }
+ if (scalar @aPath == $binaryTree->{depth}){
+ my $total =0;
+ map { $total = $total + $_} @aPath;
+ if ($total == $sum ){
+ foreach my $i (0..$binaryTree->{depth}-1){
+ print $aPath[$i].' → ';
+ }
+ print " sums $sum\n";
+ }
+ }
+}
+
+sub buildBinaryTree {
+ my $root = createNode(5);
+ $root->{left} = createNode(4);
+ $root->{left}->{left} = createNode(11);
+ $root->{left}->{left}->{left} = createNode(7);
+ $root->{left}->{left}->{right} = createNode(2);
+ $root->{right} = createNode(8);
+ $root->{right}->{left} = createNode(13);
+ $root->{right}->{right} = createNode(9);
+ $root->{right}->{right}->{right} = createNode(1);
+ return {root => $root, depth => 4};
+}
+
+sub createNode {
+ my $value = shift;
+ my %hTmp =();
+ $hTmp{value} = $value;
+ $hTmp{left} = undef;
+ $hTmp{right} = undef;
+ return \%hTmp;
+}