aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-06-08 12:31:06 +0100
committerGitHub <noreply@github.com>2020-06-08 12:31:06 +0100
commit40b9d770cca12033f9773d6e44a3e66b9c61babe (patch)
tree2ce2c22e7218208c3f5921bdf7941dc36dfd9c32
parentf2c3c84295ef3b02c765dcf5b72777892c30b9da (diff)
parentb4b337a1c7dc60f7ed0a5dd109927a28e35aed58 (diff)
downloadperlweeklychallenge-club-40b9d770cca12033f9773d6e44a3e66b9c61babe.tar.gz
perlweeklychallenge-club-40b9d770cca12033f9773d6e44a3e66b9c61babe.tar.bz2
perlweeklychallenge-club-40b9d770cca12033f9773d6e44a3e66b9c61babe.zip
Merge pull request #1801 from fluca1978/pwc64
Pwc64
-rw-r--r--challenge-064/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-064/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-064/luca-ferrari/raku/ch-1.p672
-rw-r--r--challenge-064/luca-ferrari/raku/ch-2.p643
4 files changed, 117 insertions, 0 deletions
diff --git a/challenge-064/luca-ferrari/blog-1.txt b/challenge-064/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..4a704585dd
--- /dev/null
+++ b/challenge-064/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/06/08/PerlWeeklyChallenge64.html#task1
diff --git a/challenge-064/luca-ferrari/blog-2.txt b/challenge-064/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..eeb2064fce
--- /dev/null
+++ b/challenge-064/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/06/08/PerlWeeklyChallenge64.html#task2
diff --git a/challenge-064/luca-ferrari/raku/ch-1.p6 b/challenge-064/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..72cfad7332
--- /dev/null
+++ b/challenge-064/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,72 @@
+#!raku
+
+
+# Given an m × n matrix with non-negative integers,
+# write a script to find a path from top left to bottom
+# right which minimizes the sum of all numbers along its path.
+# You can only move either down or right at any point in time.
+# Example
+#
+# Input:
+#
+# [ 1 2 3 ]
+# [ 4 5 6 ]
+# [ 7 8 9 ]
+#
+# The minimum sum path looks like this:
+#
+# 1→2→3
+# ↓
+# 6
+# ↓
+# 9
+#
+
+
+class Node {
+ has $.position;
+ has $.value;
+
+ has Node $.right is rw;
+ has Node $.down is rw;
+
+ method adjust( @matrix, $m, $n ){
+ my $right = $!position + 1;
+ my $down = $!position + $m;
+ $!right = Node.new( :position( $right ), :value( @matrix[ $right ] ) ) if $right < $m;
+ $!down = Node.new( :position( $down ), :value( @matrix[ $down ] ) ) if $down <= $n * $m - 1;
+
+ $!right.adjust: @matrix, $m, $n if $!right;
+ $!down.adjust: @matrix, $m, $n if $!down;
+ }
+
+ method get-next-min-path(){
+ return $!right if $!right && $!down && $!right.value < $!down.value;
+ return $!down;
+ }
+
+
+}
+
+
+
+
+
+sub MAIN(){
+ my @matrix = 1, 2, 3, 4, 5, 6, 7, 8, 9;
+ my $m = 3;
+ my $n = 3;
+
+
+ my $start = Node.new( :position( 0 ), :value( @matrix[ 0 ] ) );
+ $start.adjust: @matrix, $m, $n;
+ my $current-node = $start;
+ my $sum = 0;
+ my @moves;
+ while ( $current-node ) {
+ @moves.push: $current-node.value;
+ $current-node = $current-node.get-next-min-path;
+ }
+
+ say @moves.join( ', ' ) ~ " = " ~ [+] @moves;
+}
diff --git a/challenge-064/luca-ferrari/raku/ch-2.p6 b/challenge-064/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..9cb3352004
--- /dev/null
+++ b/challenge-064/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,43 @@
+#!raku
+
+# You are given a string $S and an array of words @W.
+#
+# Write a script to find out if $S can be split into sequence of one or more words as in the given @W.
+#
+# Print the all the words if found otherwise print 0.
+# Example 1:
+#
+# Input:
+#
+# $S = "perlweeklychallenge"
+# @W = ("weekly", "challenge", "perl")
+#
+# Output:
+#
+# "perl", "weekly", "challenge"
+
+
+sub MAIN( Str $S? = 'perlweeklychallenge', @W? = [ "weekly", "challenge", "perl" ] ){
+
+ my $string = $S;
+ my @found-words;
+ my $redo = True;
+
+
+ while ( $redo ) {
+ $redo = False; # if no one match, skip the next loop
+ for @W -> $part {
+ # if the current string begins with this token,
+ # mark as found and remove from the string
+ if $string ~~ / ^ $part / {
+ @found-words.push: $part;
+ $string ~~ s/ ^ $part //;
+ $redo = True;
+ }
+ }
+ }
+
+ # all done
+ "In the string $S I found the words { @found-words.join( ',' ) } ".say if @found-words;
+ '0'.say if ! @found-words;
+}