diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-01-24 00:19:18 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-01-24 00:19:18 +0000 |
| commit | 2925ccf7a400079d257e6ee2f171a86446404eea (patch) | |
| tree | ea7d3e8eac2b813a28378194127936e371e0b375 | |
| parent | 66bcf4f9207a46510dc69b52620dae1c1cd0701f (diff) | |
| download | perlweeklychallenge-club-2925ccf7a400079d257e6ee2f171a86446404eea.tar.gz perlweeklychallenge-club-2925ccf7a400079d257e6ee2f171a86446404eea.tar.bz2 perlweeklychallenge-club-2925ccf7a400079d257e6ee2f171a86446404eea.zip | |
- Added solutions by Laurent Rosenfeld.
21 files changed, 1245 insertions, 1065 deletions
diff --git a/challenge-096/laurent-rosenfeld/blog.txt b/challenge-096/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..8a78b5ca0c --- /dev/null +++ b/challenge-096/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2021/01/perl-weekly-challenge-96-reverse-words-and-edit-distance-and-decorators-in-perl.html diff --git a/challenge-096/laurent-rosenfeld/perl/ch-1.pl b/challenge-096/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..8370c20083 --- /dev/null +++ b/challenge-096/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,6 @@ +use strict; +use warnings; +use feature "say"; + +my $input = shift // " Perl and Raku are part of the same family "; +say join " ", reverse split /\s+/, $input; diff --git a/challenge-096/laurent-rosenfeld/perl/ch-2.pl b/challenge-096/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..afa8cf9ab3 --- /dev/null +++ b/challenge-096/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,66 @@ +use strict; +use warnings; +use feature qw/say/; + +sub min { + my $rv = shift; + for my $tmp (@_) { + $rv = $tmp if $tmp < $rv; + } + return $rv; +} + +sub edit_distance { + my ($left, $right) = @_; + + # If one of the substrings is empty, return the length of the other + return length $right unless $left; + return length $left unless $right; + + my $shortened_left = substr $left, 0, -1; + my $shortened_right = substr $right, 0, -1; + # In the last chars are the same, we ignore them + # and call edit_distance on shortened strings + return edit_distance ($shortened_left, $shortened_right) if substr($left, -1) eq substr($right, -1); + + # Else find the minimum between the three operations + return 1 + min( + edit_distance($left, $shortened_right), #insert + edit_distance($shortened_left, $right), #remove + edit_distance($shortened_left, $shortened_right) #replace + ); +} + +# The decorator returns the edit_distance subroutine wrapped in +# code lines performing the caching of values +sub decorate { + my $coderef = shift; + my %cache; + return sub { + my ($l, $r) = @_; + $cache{"$l;$r"} = $coderef->(@_) unless exists $cache{"$l;$r"}; + return $cache{"$l;$r"}; + } +} + +{ + # local scope for the deactivation of the redefine warning + no warnings 'redefine'; + # we replace the edit-distance subrouytine by its + # decorated version in the main symbol table + *main::edit_distance = decorate(\&edit_distance); +} + +my @test_pairs = ( + [ qw<kitten sitting> ], + [ qw<Monday Friday> ], + [ qw<Sunday Saturday> ], + [ qw<January February> ], + [ qw<November December > ], + [ qw<constitutionally anticonstitutional > ], + [ qw<LMIjkHFSAE dmqkdjfERZG>], +); +for my $ar (@test_pairs) { + my ($str1, $str2) = @$ar; + say edit_distance($str1,$str2), " $str1 - $str2 "; +} diff --git a/challenge-096/laurent-rosenfeld/python/ch-1.py b/challenge-096/laurent-rosenfeld/python/ch-1.py new file mode 100644 index 0000000000..317f557175 --- /dev/null +++ b/challenge-096/laurent-rosenfeld/python/ch-1.py @@ -0,0 +1,7 @@ +def reverse_words(in_str): + words = in_str.split() + words.reverse() + return " ".join(words) + +input = " Perl and Raku are part of the same family " +print(reverse_words(input)) diff --git a/challenge-096/laurent-rosenfeld/python/ch-2.py b/challenge-096/laurent-rosenfeld/python/ch-2.py new file mode 100644 index 0000000000..363251c03f --- /dev/null +++ b/challenge-096/laurent-rosenfeld/python/ch-2.py @@ -0,0 +1,31 @@ +cache = dict() + +def cached_distance(left, right): + key = left + ';' + right + if key not in cache: + cache[key] = edit_distance(left, right) + return cache[key] + +def edit_distance(left, right): + lr = len(right) + ll = len(left) + if not left: return lr + if not right: return ll + shortened_l = left[0:ll-1] + shortened_r = right[0:lr-1] + if left[ll-1] == right[lr-1]: + return cached_distance(shortened_l, shortened_r) + + return 1 + min( cached_distance(left, shortened_r), # Insert + cached_distance(shortened_l, right), # Remove + cached_distance(shortened_l, shortened_r) # Replace + ) + +tests = ( [ "kitten", "sitting" ], [ "Monday", "Friday" ], + [ "Sunday", "Saturday" ], [ "January", "February" ], + [ "November", "December" ], + [ "constitutionally", "anticonstitutional" ], + ) + +for test in tests: + print (test[0], test[1], edit_distance(test[0], test[1] )) diff --git a/challenge-096/laurent-rosenfeld/raku/ch-1.raku b/challenge-096/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..0c10b69dec --- /dev/null +++ b/challenge-096/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,4 @@ +use v6; + +my $input = @*ARGS[0] // " Perl and Raku are part of the same family "; +say $input.words.reverse.join(" "); diff --git a/challenge-096/laurent-rosenfeld/raku/ch-2.raku b/challenge-096/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..b30d8ae8c5 --- /dev/null +++ b/challenge-096/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,38 @@ +use v6; +use Memoize; + +sub edit-distance (Str $left, Str $right) { + # If one of the substrings is empty, return the length of the other + return $right.chars unless $left; + return $left.chars unless $right; + my $shortened-left = substr $left, 0, *-1; + my $shortened-right = substr $right, 0, *-1; + + # If the last chars are the same, we ignore them + # and call edit-distance on shortened strings + return edit-distance $shortened-left, $shortened-right + if substr($left, *-1) eq substr($right, *-1); + + # Else find the minimum between the three operations + return 1 + min( + edit-distance($left, $shortened-right), #insert + edit-distance($shortened-left, $right), #remove + edit-distance($shortened-left, $shortened-right) #replace + ); +} + +memoize("edit-distance"); + +my @test-pairs = ( + [ < kitten sitting >], + [ < Monday Friday > ], + [ < Sunday Saturday > ], + [ < January February > ], + [ < November December > ], + [ < constitutionally anticonstitutional > ], + [ < LMIjkHFSAE dmqkdjfERZG >], +); +for @test-pairs -> @test { + my ($str1, $str2) = @test; + print edit-distance($str1, $str2), " $str1 - $str2\n"; +} diff --git a/challenge-096/laurent-rosenfeld/scala/ch-1.scala b/challenge-096/laurent-rosenfeld/scala/ch-1.scala new file mode 100644 index 0000000000..6df5d74c3f --- /dev/null +++ b/challenge-096/laurent-rosenfeld/scala/ch-1.scala @@ -0,0 +1,4 @@ +object reverseWords extends App { + val in = " Perl and Raku are part of the same family " + println(in.split("\\s+").reverse.mkString(" ")) +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 7201810974..f5ea2ca7c2 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,51 +1,38 @@ { - "chart" : { - "type" : "column" - }, - "subtitle" : { - "text" : "[Champions: 26] Last updated at 2021-01-23 23:50:12 GMT" - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "legend" : { - "enabled" : 0 - }, "series" : [ { "colorByPoint" : 1, + "name" : "Perl Weekly Challenge - 096", "data" : [ { - "name" : "Aaron Smith", "y" : 3, - "drilldown" : "Aaron Smith" + "drilldown" : "Aaron Smith", + "name" : "Aaron Smith" }, { - "drilldown" : "Abigail", "y" : 4, + "drilldown" : "Abigail", "name" : "Abigail" }, { - "name" : "Andrew Shitov", "drilldown" : "Andrew Shitov", + "name" : "Andrew Shitov", "y" : 1 }, { - "name" : "Arne Sommer", + "y" : 5, "drilldown" : "Arne Sommer", - "y" : 5 + "name" : "Arne Sommer" }, { - "y" : 2, "drilldown" : "Ben Davies", - "name" : "Ben Davies" + "name" : "Ben Davies", + "y" : 2 }, { - "name" : "Cheok-Yin Fung", + "y" : 2, "drilldown" : "Cheok-Yin Fung", - "y" : 2 + "name" : "Cheok-Yin Fung" }, { "name" : "Dave Jacoby", @@ -53,34 +40,34 @@ "y" : 3 }, { - "name" : "E. Choroba", "y" : 2, + "name" : "E. Choroba", "drilldown" : "E. Choroba" }, { - "drilldown" : "Flavio Poletti", "y" : 4, + "drilldown" : "Flavio Poletti", "name" : "Flavio Poletti" }, { - "y" : 2, + "name" : "Gustavo Chaves", "drilldown" : "Gustavo Chaves", - "name" : "Gustavo Chaves" + "y" : 2 }, { - "drilldown" : "James Smith", "y" : 2, + "drilldown" : "James Smith", "name" : "James Smith" }, { + "drilldown" : "Joan Mimosinnet", "name" : "Joan Mimosinnet", - "y" : 3, - "drilldown" : "Joan Mimosinnet" + "y" : 3 }, { - "y" : 2, + "name" : "Jorg Sommrey", "drilldown" : "Jorg Sommrey", - "name" : "Jorg Sommrey" + "y" : 2 }, { "name" : "Kang-min Liu", @@ -88,80 +75,86 @@ "y" : 5 }, { + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", + "y" : 5 + }, + { "name" : "Lubos Kolouch", "drilldown" : "Lubos Kolouch", "y" : 2 }, { "y" : 4, - "drilldown" : "Luca Ferrari", - "name" : "Luca Ferrari" + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari" }, { - "name" : "Mark Anderson", "y" : 2, + "name" : "Mark Anderson", "drilldown" : "Mark Anderson" }, { - "name" : "Nuno Vieira", "y" : 2, - "drilldown" : "Nuno Vieira" + "drilldown" : "Nuno Vieira", + "name" : "Nuno Vieira" }, { - "name" : "Paulo Custodio", "drilldown" : "Paulo Custodio", + "name" : "Paulo Custodio", "y" : 2 }, { - "y" : 2, "drilldown" : "Pete Houston", - "name" : "Pete Houston" + "name" : "Pete Houston", + "y" : 2 }, { - "y" : 2, "drilldown" : "Philip Hood", - "name" : "Philip Hood" + "name" : "Philip Hood", + "y" : 2 }, { - "y" : 5, "drilldown" : "Roger Bell_West", - "name" : "Roger Bell_West" + "name" : "Roger Bell_West", + "y" : 5 }, { - "name" : "Simon Proctor", "y" : 2, + "name" : "Simon Proctor", "drilldown" : "Simon Proctor" }, { + "name" : "Stuart Little", "drilldown" : "Stuart Little", - "y" : 4, - "name" : "Stuart Little" + "y" : 4 }, { - "y" : 4, "drilldown" : "Ulrich Rieke", - "name" : "Ulrich Rieke" + "name" : "Ulrich Rieke", + "y" : 4 }, { "y" : 3, - "drilldown" : "W. Luis Mochan", - "name" : "W. Luis Mochan" + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan" } - ], - "name" : "Perl Weekly Challenge - 096" + ] } ], - "title" : { - "text" : "Perl Weekly Challenge - 096" - }, - "xAxis" : { - "type" : "category" + "plotOptions" : { + "series" : { + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + }, + "borderWidth" : 0 + } }, "drilldown" : { "series" : [ { "id" : "Aaron Smith", - "name" : "Aaron Smith", "data" : [ [ "Raku", @@ -171,7 +164,8 @@ "Blog", 1 ] - ] + ], + "name" : "Aaron Smith" }, { "data" : [ @@ -194,10 +188,12 @@ 1 ] ], - "id" : "Andrew Shitov", - "name" : "Andrew Shitov" + "name" : "Andrew Shitov", + "id" : "Andrew Shitov" }, { + "id" : "Arne Sommer", + "name" : "Arne Sommer", "data" : [ [ "Perl", @@ -211,31 +207,31 @@ "Blog", 1 ] - ], - "id" : "Arne Sommer", - "name" : "Arne Sommer" + ] }, { - "name" : "Ben Davies", - "id" : "Ben Davies", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Ben Davies", + "id" : "Ben Davies" }, { - "id" : "Cheok-Yin Fung", - "name" : "Cheok-Yin Fung", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Cheok-Yin Fung", + "id" : "Cheok-Yin Fung" }, { + "id" : "Dave Jacoby", + "name" : "Dave Jacoby", "data" : [ [ "Perl", @@ -245,21 +241,21 @@ "Blog", 1 ] - ], - "name" : "Dave Jacoby", - "id" : "Dave Jacoby" + ] }, { - "id" : "E. Choroba", - "name" : "E. Choroba", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "E. Choroba", + "id" : "E. Choroba" }, { + "id" : "Flavio Poletti", + "name" : "Flavio Poletti", "data" : [ [ "Perl", @@ -269,29 +265,27 @@ "Blog", 2 ] - ], - "name" : "Flavio Poletti", - "id" : "Flavio Poletti" + ] }, { - "name" : "Gustavo Chaves", "id" : "Gustavo Chaves", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Gustavo Chaves" }, { "id" : "James Smith", - "name" : "James Smith", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "James Smith" }, { "data" : [ @@ -304,22 +298,40 @@ 1 ] ], - "id" : "Joan Mimosinnet", - "name" : "Joan Mimosinnet" + "name" : "Joan Mimosinnet", + "id" : "Joan Mimosinnet" }, { + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey", "data" : [ [ "Perl", 2 ] - ], - "name" : "Jorg Sommrey", - "id" : "Jorg Sommrey" + ] }, { - "id" : "Kang-min Liu", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], "name" : "Kang-min Liu", + "id" : "Kang-min Liu" + }, + { + "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -336,16 +348,17 @@ ] }, { - "id" : "Lubos Kolouch", - "name" : "Lubos Kolouch", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Lubos Kolouch", + "id" : "Lubos Kolouch" }, { + "name" : "Luca Ferrari", "data" : [ [ "Raku", @@ -356,42 +369,41 @@ 2 ] ], - "name" : "Luca Ferrari", "id" : "Luca Ferrari" }, { - "name" : "Mark Anderson", "id" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Mark Anderson" }, { + "name" : "Nuno Vieira", "data" : [ [ "Perl", 2 ] ], - "id" : "Nuno Vieira", - "name" : "Nuno Vieira" + "id" : "Nuno Vieira" }, { "name" : "Paulo Custodio", - "id" : "Paulo Custodio", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Paulo Custodio" }, { - "name" : "Pete Houston", "id" : "Pete Houston", + "name" : "Pete Houston", "data" : [ [ "Perl", @@ -400,16 +412,17 @@ ] }, { - "id" : "Philip Hood", - "name" : "Philip Hood", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Philip Hood", + "id" : "Philip Hood" }, { + "name" : "Roger Bell_West", "data" : [ [ "Perl", @@ -424,21 +437,19 @@ 1 ] ], - "name" : "Roger Bell_West", "id" : "Roger Bell_West" }, { + "id" : "Simon Proctor", + "name" : "Simon Proctor", "data" : [ [ "Raku", 2 ] - ], - "id" : "Simon Proctor", - "name" : "Simon Proctor" + ] }, { - "id" : "Stuart Little", "name" : "Stuart Little", "data" : [ [ @@ -449,7 +460,8 @@ "Raku", 2 ] - ] + ], + "id" : "Stuart Little" }, { "id" : "Ulrich Rieke", @@ -467,7 +479,6 @@ }, { "name" : "W. Luis Mochan", - "id" : "W. Luis Mochan", "data" : [ [ "Perl", @@ -477,22 +488,34 @@ "Blog", 1 ] - ] + ], + "id" : "W. Luis Mochan" } ] }, - "plotOptions" : { - "series" : { - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - }, - "borderWidth" : 0 - } - }, "tooltip" : { "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", - "followPointer" : 1, - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", + "followPointer" : 1 + }, + "subtitle" : { + "text" : "[Champions: 27] Last updated at 2021-01-24 00:19:00 GMT" + }, + "legend" : { + "enabled" : 0 + }, + "xAxis" : { + "type" : "category" + }, + "chart" : { + "type" : "column" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "title" : { + "text" : "Perl Weekly Challenge - 096" } } diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index 6cd2f710cd..0431f1c19a 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -1,63 +1,63 @@ { - "title" : { - "text" : "Perl Weekly Challenge Contributions [2019 - 2020]" - }, "series" : [ { - "name" : "Contributions", + "dataLabels" : { + "rotation" : -90, + "align" : "right", + "y" : 10, + "style" : { + "fontFamily" : "Verdana, sans-serif", + "fontSize" : "13px" + }, + "format" : "{point.y:.0f}", + "enabled" : "true", + "color" : "#FFFFFF" + }, "data" : [ [ "Blog", - 1276 + 1277 ], [ "Perl", - 4450 + 4452 ], [ "Raku", - 2901 + 2903 ] ], - "dataLabels" : { - "rotation" : -90, - "color" : "#FFFFFF", - "y" : 10, - "format" : "{point.y:.0f}", - "align" : "right", - "style" : { - "fontFamily" : "Verdana, sans-serif", - "fontSize" : "13px" - }, - "enabled" : "true" - } + "name" : "Contributions" } ], - "legend" : { - "enabled" : "false" - }, - "yAxis" : { - "title" : { - "text" : null - }, - "min" : 0 - }, - "subtitle" : { - "text" : "Last updated at 2021-01-23 23:50:11 GMT" - }, - "chart" : { - "type" : "column" - }, "tooltip" : { "pointFormat" : "<b>{point.y:.0f}</b>" }, "xAxis" : { - "type" : "category", "labels" : { "style" : { "fontFamily" : "Verdana, sans-serif", "fontSize" : "13px" } - } + }, + "type" : "category" + }, + "chart" : { + "type" : "column" + }, + "subtitle" : { + "text" : "Last updated at 2021-01-24 00:19:00 GMT" + }, + "legend" : { + "enabled" : "false" + }, + "title" : { + "text" : "Perl Weekly Challenge Contributions [2019 - 2020]" + }, + "yAxis" : { + "title" : { + "text" : null + }, + "min" : 0 } } diff --git a/stats/pwc-language-breakdown.json b/stats/pwc-language-breakdown.json index c42596d223..2294f6af89 100644 --- a/stats/pwc-language-breakdown.json +++ b/stats/pwc-language-breakdown.json @@ -1,75 +1,81 @@ { - "title" : { - "text" : "Perl Weekly Challenge Language" + "plotOptions" : { + "series" : { + "borderWidth" : 0, + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + } + } }, "series" : [ { - "colorByPoint" : "true", + "name" : "Perl Weekly Challenge Languages", "data" : [ { + "drilldown" : "001", "name" : "#001", - "y" : 153, - "drilldown" : "001" + "y" : 153 }, { + "y" : 118, "name" : "#002", - "drilldown" : "002", - "y" : 118 + "drilldown" : "002" }, { - "drilldown" : "003", "y" : 75, + "drilldown" : "003", "name" : "#003" }, { - "drilldown" : "004", "y" : 95, + "drilldown" : "004", "name" : "#004" }, { + "drilldown" : "005", "name" : "#005", - "y" : 76, - "drilldown" : "005" + "y" : 76 }, { - "name" : "#006", + "y" : 56, "drilldown" : "006", - "y" : 56 + "name" : "#006" }, { "name" : "#007", - "y" : 63, - "drilldown" : "007" + "drilldown" : "007", + "y" : 63 }, { - "name" : "#008", "y" : 76, + "name" : "#008", "drilldown" : "008" }, { "y" : 74, - "drilldown" : "009", - "name" : "#009" + "name" : "#009", + "drilldown" : "009" }, { "drilldown" : "010", - "y" : 64, - "name" : "#010" + |
