aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-056/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-056/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-056/luca-ferrari/raku/ch-1.p627
-rw-r--r--challenge-056/luca-ferrari/raku/ch-2.p661
4 files changed, 90 insertions, 0 deletions
diff --git a/challenge-056/luca-ferrari/blog-1.txt b/challenge-056/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..3754eac35a
--- /dev/null
+++ b/challenge-056/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/04/14/PerlWeeklyChallenge56.html#task1
diff --git a/challenge-056/luca-ferrari/blog-2.txt b/challenge-056/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..a47482cc2d
--- /dev/null
+++ b/challenge-056/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/04/14/PerlWeeklyChallenge56.html#task2
diff --git a/challenge-056/luca-ferrari/raku/ch-1.p6 b/challenge-056/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..7219519666
--- /dev/null
+++ b/challenge-056/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,27 @@
+#!env raku
+
+# 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
+
+
+sub MAIN( Int:D :$K, *@N ) {
+ say "Array { @N } and index $K";
+ for 0 ..^ @N.elems -> $i {
+ for $i ^..^ @N.elems -> $j {
+ say "$i, $j ---> { @N[ $i ] } <-> { @N[ $j ] }"
+ if ( @N[ $i ] - @N[ $j ] == any( $K, $K * -1 ) )
+ }
+ }
+}
diff --git a/challenge-056/luca-ferrari/raku/ch-2.p6 b/challenge-056/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..0bcfb1ec73
--- /dev/null
+++ b/challenge-056/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,61 @@
+#!env raku
+
+
+# 5
+# / \
+# 4 8
+# / / \
+# 11 13 9
+# / \ \
+# 7 2 1
+
+
+class Node {
+ has Int:D $.value = 0;
+ has Node $.left is rw;
+ has Node $.right is rw;
+ has Node $.parent is rw;
+ has Bool $.is-leaf = False;
+}
+
+
+sub MAIN() {
+ my $target = 22;
+
+ my $root = Node.new( :value( 5 ) );
+ $root.left = Node.new( :value( 4 ), :parent( $root ) );
+ $root.right = Node.new( :value( 8 ), :parent( $root ) );
+ $root.left.left = Node.new( :value( 11 ), :parent( $root.left ) );
+ $root.right.left = Node.new( :value( 13 ), :parent( $root.right ), :is-leaf );
+ $root.right.right = Node.new( :value( 9 ), :parent( $root.right ) );
+ $root.left.left.left = Node.new( :value( 7 ), :parent( $root.left.left ), :is-leaf );
+ $root.left.left.right = Node.new( :value( 2 ), :parent( $root.left.left ), :is-leaf );
+ $root.right.right.right = Node.new( :value( 1 ), :parent( $root.right.right ), :is-leaf );
+
+
+ my @nodes = $root
+ , $root.left
+ , $root.right
+ , $root.left.left
+ , $root.right.left
+ , $root.right.right
+ , $root.left.left.left
+ , $root.left.left.right
+ , $root.right.right.right;
+
+
+
+ # find the leaves
+ my @leaves = @nodes.grep( *.is-leaf );
+
+
+ # now walk from the leaves to the root
+ for @leaves {
+ my @path = [ .value ];
+ my $node = $_;
+ @path.push: $node.value while ( $node = $node.parent );
+ my $sum = [+] @path;
+ say "Sum is $sum with the path { @path.reverse }" if ( $sum == $target );
+ }
+
+}