aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-129/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-129/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-129/luca-ferrari/raku/ch-1.p641
-rw-r--r--challenge-129/luca-ferrari/raku/ch-2.p686
-rw-r--r--challenge-129/luca-ferrari/raku/ch-2b.p621
5 files changed, 150 insertions, 0 deletions
diff --git a/challenge-129/luca-ferrari/blog-1.txt b/challenge-129/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..e4de8d4c55
--- /dev/null
+++ b/challenge-129/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/09/06/PerlWeeklyChallenge129.html#task1
diff --git a/challenge-129/luca-ferrari/blog-2.txt b/challenge-129/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..90adfebb00
--- /dev/null
+++ b/challenge-129/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/09/06/PerlWeeklyChallenge129.html#task2
diff --git a/challenge-129/luca-ferrari/raku/ch-1.p6 b/challenge-129/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..692ae15af4
--- /dev/null
+++ b/challenge-129/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,41 @@
+#!raku
+
+
+class Node {
+ has Node $.left is rw;
+ has Node $.right is rw;
+
+ has Int $.value;
+ has Int $.level = 0;
+
+ method is-leaf() { ! $!left && ! $!right }
+
+ method find-from-here( Int $needle ) {
+ return self if $!value == $needle;
+
+ my $r = $!right.find-from-here( $needle ) if $!right;
+ return $r if $r;
+ my $l = $!left.find-from-here( $needle ) if $!left;
+ return $l if $l;
+ return Nil if self.is-leaf;
+
+ }
+}
+
+
+sub MAIN( Int $needle = 6 ) {
+ my $level = 1;
+ my $root = Node.new( value => 1,
+ left => Node.new( value => 2, level => $level ),
+ right => Node.new( value => 3, level => $level ) );
+ $root.right.right = Node.new( value => 4, level => ++$level );
+ $level++;
+ $root.right.right.right = Node.new( value => 6, level => $level );
+ $root.right.right.left = Node.new( value => 5, level => $level );
+
+
+
+ my $current = $root.find-from-here( $needle );
+ say "Found $needle at distance { $current.level }" if $current;
+ say "Not found $needle" if ! $current;
+}
diff --git a/challenge-129/luca-ferrari/raku/ch-2.p6 b/challenge-129/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..2863546afb
--- /dev/null
+++ b/challenge-129/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,86 @@
+#!raku
+
+class LL {
+ has Int $.value = -1;
+ has Bool $.available is rw = True;
+ has LL $.next is rw = LL;
+
+
+ method length() {
+ my $counter = 1;
+ my $current = self;
+ $counter++ && $current = $current.next while ( $current.next );
+ return $counter;
+ }
+
+
+ method pop-last() {
+ my $current = self;
+ return Nil if ! $current.available;
+
+ while ( $current.available && $current.next && $current.next.available ) {
+ $current = $current.next;
+ }
+
+ $current.available = False;
+ return $current;
+ }
+
+ method print() {
+ print $!value;
+ if ( $!next ) {
+ print " -> ";
+ $!next.print;
+ }
+ }
+}
+
+
+sub MAIN() {
+ my $L1 = LL.new( value => 1 );
+ $L1.next = LL.new( value => 2 );
+ $L1.next.next = LL.new( value => 3 );
+ $L1.next.next.next = LL.new( value => 4 );
+ $L1.next.next.next.next = LL.new( value => 5 );
+
+ my $L2 = LL.new( value => 6 );
+ $L2.next = LL.new( value => 5 );
+ $L2.next.next = LL.new( value => 5 );
+
+ say "L1 = " ~ $L1.length;
+ say "L2 = " ~ $L2.length;
+
+ my ( $sum, $carry ) = 0, 0;
+ my @sums;
+ for 0 ..^ max( $L1.length, $L2.length ) {
+ my ( $a, $b ) = ( $L1.pop-last, $L2.pop-last );
+ my $sum = $carry
+ + ( $a ?? $a.value !! 0 )
+ + ( $b ?? $b.value !! 0 );
+
+ if ( $sum >= 10 ) {
+ $carry = ( $sum / 10 ).Int;
+ $sum %= 10;
+ }
+ else {
+ $carry = 0;
+ }
+
+ @sums.push: $sum;
+ }
+
+
+ my $R;
+ my $current;
+ my $previous;
+ for @sums {
+ $current = LL.new( value => $_, next => ( $previous ?? $previous !! Nil ) );
+ $previous = $current;
+ }
+
+ say $current.print;
+
+
+
+
+}
diff --git a/challenge-129/luca-ferrari/raku/ch-2b.p6 b/challenge-129/luca-ferrari/raku/ch-2b.p6
new file mode 100644
index 0000000000..e9971214f7
--- /dev/null
+++ b/challenge-129/luca-ferrari/raku/ch-2b.p6
@@ -0,0 +1,21 @@
+#!raku
+
+
+sub MAIN() {
+ my @L1 = 1 , 2 , 3 , 4 , 5;
+ my @L2 = 6 , 5 , 5;
+
+ my @sums;
+ my $carry = 0;
+
+ for 1 .. max( @L1.elems, @L2.elems ) {
+ my $sum = ( @L1.elems >= $_ ?? @L1[ * - $_ ] !! 0 )
+ + ( @L2.elems >= $_ ?? @L2[ * - $_ ] !! 0 )
+ + $carry;
+ $carry = 0;
+ ( $sum, $carry ) = ( $sum % 10, ( $sum / 10 ).Int ) if $sum >= 10;
+ @sums.push: $sum;
+ }
+
+ @sums.join( ' -> ' ).say;
+}