From c4708467abd49747d2a660d44bcbecf07d0c52b7 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 8 Jun 2020 11:12:49 +0200 Subject: Task 2 done. --- challenge-064/luca-ferrari/raku/ch-2.p6 | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-064/luca-ferrari/raku/ch-2.p6 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; +} -- cgit From 0e8a9a28fb78db2f8daaa7cc882b191cfbbb95b4 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 8 Jun 2020 12:00:46 +0200 Subject: Task 1 done --- challenge-064/luca-ferrari/raku/ch-1.p6 | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 challenge-064/luca-ferrari/raku/ch-1.p6 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..bdce99d3a6 --- /dev/null +++ b/challenge-064/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,70 @@ +#!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; +} -- cgit From b4b337a1c7dc60f7ed0a5dd109927a28e35aed58 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 8 Jun 2020 12:22:37 +0200 Subject: Blog references. --- challenge-064/luca-ferrari/blog-1.txt | 1 + challenge-064/luca-ferrari/blog-2.txt | 1 + challenge-064/luca-ferrari/raku/ch-1.p6 | 2 ++ 3 files changed, 4 insertions(+) create mode 100644 challenge-064/luca-ferrari/blog-1.txt create mode 100644 challenge-064/luca-ferrari/blog-2.txt 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 index bdce99d3a6..72cfad7332 100644 --- a/challenge-064/luca-ferrari/raku/ch-1.p6 +++ b/challenge-064/luca-ferrari/raku/ch-1.p6 @@ -44,6 +44,8 @@ class Node { return $!right if $!right && $!down && $!right.value < $!down.value; return $!down; } + + } -- cgit