diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-06-14 17:25:46 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-06-14 17:25:46 +0100 |
| commit | 4d37bd51e00fb61f449cff5a9e554b4ddce1ed78 (patch) | |
| tree | f32b9dc046da5fd1308babd7f365593427ff9113 /challenge-064 | |
| parent | 08179f9c314eeff8c425d2078b46b6d8c9237b7b (diff) | |
| download | perlweeklychallenge-club-4d37bd51e00fb61f449cff5a9e554b4ddce1ed78.tar.gz perlweeklychallenge-club-4d37bd51e00fb61f449cff5a9e554b4ddce1ed78.tar.bz2 perlweeklychallenge-club-4d37bd51e00fb61f449cff5a9e554b4ddce1ed78.zip | |
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-064')
| -rw-r--r-- | challenge-064/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-064/laurent-rosenfeld/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-064/laurent-rosenfeld/perl/ch-2.pl | 16 | ||||
| -rw-r--r-- | challenge-064/laurent-rosenfeld/raku/ch-1.p6 | 26 | ||||
| -rw-r--r-- | challenge-064/laurent-rosenfeld/raku/ch-2.p6 | 14 |
5 files changed, 88 insertions, 0 deletions
diff --git a/challenge-064/laurent-rosenfeld/blog.txt b/challenge-064/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..074de71acf --- /dev/null +++ b/challenge-064/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2020/06/perl-weekly-challenge-64-minimum-sum-path-and-word-break.html diff --git a/challenge-064/laurent-rosenfeld/perl/ch-1.pl b/challenge-064/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..9f6a23d082 --- /dev/null +++ b/challenge-064/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; +use feature qw/say/; + +my @mat = ([qw<7 8 9>], [qw<1 2 3>], [qw<4 5 6>] ); +my @best_path; +my $min = 0; +for my $row (@mat) { + $min += $_ for @$row; +} +my @empty_path; + +traverse_mat(0, 0, 0, ()); + +sub traverse_mat { + my ($i, $j, $sum, @path) = @_; + my $new_sum = $sum + $mat[$i][$j]; + return if $new_sum > $min; + my @new_path = (@path, $mat[$i][$j]); + if (defined $mat[$i][$j+1]) { + traverse_mat($i, $j+1, $new_sum, @new_path); + } + if (defined $mat[$i+1][$j]) { + traverse_mat($i+1, $j, $new_sum, @new_path); + } + unless (defined $mat[$i][$j+1] or defined $mat[$i+1][$j]) { + @best_path = @new_path; + $min = $new_sum; + } +} +say $min, " (", join(' → ', @best_path), ")"; diff --git a/challenge-064/laurent-rosenfeld/perl/ch-2.pl b/challenge-064/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..e65e96d8ab --- /dev/null +++ b/challenge-064/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,16 @@ +use strict; +use warnings; +use feature qw/say/; + +my $string = "perlweeklychallenge"; +my @words = <weekly challenge week perl>; +my %loc; +for my $word (@words) { + my $index = index $string, $word; + $loc{$word} = $index if $index >= 0; +} +if (%loc == 0) { + say "0"; +} else { + say join " ", sort { $loc{$a} <=> $loc{$b} } keys %loc; +} diff --git a/challenge-064/laurent-rosenfeld/raku/ch-1.p6 b/challenge-064/laurent-rosenfeld/raku/ch-1.p6 new file mode 100644 index 0000000000..55f25ac94b --- /dev/null +++ b/challenge-064/laurent-rosenfeld/raku/ch-1.p6 @@ -0,0 +1,26 @@ +use v6; + +my @mat = (<7 8 9>, <1 2 3>, <4 5 6>, ); +# say @mat; +my @best-path; +my $min = Inf; +my @empty-path; + +traverse-mat(0, 0, 0, @empty-path); + +sub traverse-mat (UInt $i, UInt $j, UInt $sum, @path is copy) { + my $new-sum = $sum + @mat[$i][$j]; + return if $new-sum > $min; + push @path, @mat[$i][$j]; + if @mat[$i][$j+1].defined { + traverse-mat($i, $j+1, $new-sum, @path); + } + if @mat[$i+1][$j].defined { + traverse-mat($i+1, $j, $new-sum, @path); + } + unless (@mat[$i][$j+1].defined or @mat[$i+1][$j].defined) { + @best-path = @path; + $min = $new-sum; + } +} +say $min, " (", join(' → ', @best-path), ")"; diff --git a/challenge-064/laurent-rosenfeld/raku/ch-2.p6 b/challenge-064/laurent-rosenfeld/raku/ch-2.p6 new file mode 100644 index 0000000000..a973e85b67 --- /dev/null +++ b/challenge-064/laurent-rosenfeld/raku/ch-2.p6 @@ -0,0 +1,14 @@ +use v6; + +my $string = "perlweeklychallenge"; +my @words = <weekly challenge week perl>; +my %location; +for @words -> $word { + my $index = index $string, $word; + push %location, $word => $index if $index.defined;; +} +if %location.elems == 0 { + say "0" +} else { + print "{$_.key} " for %location.sort({.value}); +} |
