diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-05-11 14:38:25 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-05-11 14:38:25 +0100 |
| commit | b2998ca96896e0a89935bb9b87f86ea1621e2f09 (patch) | |
| tree | 1809efbe8d1004052fd2e60ae6544ed35d4098c3 | |
| parent | 54d549c61f80e8a98104290ce981779e25d6b75e (diff) | |
| download | perlweeklychallenge-club-b2998ca96896e0a89935bb9b87f86ea1621e2f09.tar.gz perlweeklychallenge-club-b2998ca96896e0a89935bb9b87f86ea1621e2f09.tar.bz2 perlweeklychallenge-club-b2998ca96896e0a89935bb9b87f86ea1621e2f09.zip | |
- Added solutions by Jan Krnavek.
- Added solutions by Robbie Hatley.
- Added solutions by Packy Anderson.
- Added solutions by Luca Ferrari.
- Added solutions by Asher Harvey-Smith.
- Added solutions by Jorg Sommrey.
- Added solutions by Nelo Tovar.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Reinier Maliepaard.
26 files changed, 2421 insertions, 2089 deletions
diff --git a/challenge-268/laurent-rosenfeld/blog.txt b/challenge-268/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..bb5292ff01 --- /dev/null +++ b/challenge-268/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/05/perl-weekly-challenge-268-magic-numbers.html diff --git a/challenge-268/laurent-rosenfeld/blog1.txt b/challenge-268/laurent-rosenfeld/blog1.txt new file mode 100644 index 0000000000..3b49873d8c --- /dev/null +++ b/challenge-268/laurent-rosenfeld/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/05/perl-weekly-challenge-268-number-game.html diff --git a/challenge-268/laurent-rosenfeld/perl/ch-1.pl b/challenge-268/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..dbdf916756 --- /dev/null +++ b/challenge-268/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +use feature 'say'; + +sub magic_nr { + my @in1 = sort {$a<=>$b} @{$_[0]}; + my @in2 = sort {$a<=>$b} @{$_[1]}; + my $gap = $in1[0] - $in2[0]; + for my $i (1..$#in1) { + return "undef" if $in1[$i] - $in2[$i] != $gap; + } + return abs $gap; +} + +my @tests = ([[<3 7 5>], [<9 5 7>]], [[<1 2 1>], [<5 4 4>]], + [[2,], [5]], [[<3 7 5>], [<6 5 7>]] ); + +for my $test (@tests) { + printf "%-6s - %-6s => ", "@{$test->[0]}", "@{$test->[1]}"; + say magic_nr $test->[0], $test->[1]; +} diff --git a/challenge-268/laurent-rosenfeld/perl/ch-2.pl b/challenge-268/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..9522930f4a --- /dev/null +++ b/challenge-268/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +use feature 'say'; + +sub number_game { + my @in = sort { $a <=> $b } @_; + my @result; + while (@in) { + my $i = shift @in; + my $j = shift @in; + push @result, $j, $i; + } + + return join " ", @result; +} + +my @tests = ([<2 5 3 4>], [<1 1 4 3 6 4 9 6>], [<1 2 2 3>]); +for my $test (@tests) { + printf "%-16s => ", "@$test"; + say number_game @$test; +} diff --git a/challenge-268/laurent-rosenfeld/raku/ch-1.raku b/challenge-268/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..ad9c22ed51 --- /dev/null +++ b/challenge-268/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,14 @@ +sub magic-nr (@x, @y) { + my @in1 = @x.sort; + my @in2 = @y.sort; + my @gaps = map {@in1[$_] - @in2[$_]}, 0..@x.end; + return Nil unless [==] @gaps; + return @gaps[0].abs; +} + +my @tests = (<3 7 5>, <9 5 7>), (<1 2 1>, <5 4 4>), + ((2,), (5,)), (<3 7 5>, <6 5 7>); +for @tests -> @test { + printf "%-6s - %-6s => ", "@test[0]", "@test[1]"; + say magic-nr @test[0], @test[1]; +} diff --git a/challenge-268/laurent-rosenfeld/raku/ch-2.raku b/challenge-268/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..c14786c055 --- /dev/null +++ b/challenge-268/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,13 @@ +sub number-game (@in) { + my @result; + for @in.sort -> $i, $j { + push @result, $j, $i; + } + return @result; +} + +my @tests = <2 5 3 4>, <1 1 4 3 6 4 9 6>, <1 2 2 3>; +for @tests -> @test { + printf "%-16s => ", "@test[]"; + say number-game @test; +} diff --git a/challenge-268/nelo-tovar/bash/2 b/challenge-268/nelo-tovar/bash/2 deleted file mode 100644 index ac098b11e8..0000000000 --- a/challenge-268/nelo-tovar/bash/2 +++ /dev/null @@ -1 +0,0 @@ -i=2 diff --git a/challenge-268/reinier-maliepaard/blog.txt b/challenge-268/reinier-maliepaard/blog.txt new file mode 100644 index 0000000000..8cc2bcf2d4 --- /dev/null +++ b/challenge-268/reinier-maliepaard/blog.txt @@ -0,0 +1 @@ +https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc268 diff --git a/challenge-268/reinier-maliepaard/perl/ch-1.pl b/challenge-268/reinier-maliepaard/perl/ch-1.pl new file mode 100644 index 0000000000..b0863ccdad --- /dev/null +++ b/challenge-268/reinier-maliepaard/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl +use strict; +use warnings; + +I tried to find a solution without using the obvious 'sort'. +The solution below is not efficient, but easy to understand and it +does the job well + +sub magic_number { + + # define two arrays + my ($arr_1_ref, $arr_2_ref) = @_; + + # by dereferencing + my @arr_1 = @$arr_1_ref; + my @arr_2 = @$arr_2_ref; + + # check if arrays are of the same size + die "Arrays must be of the same size" unless @arr_1 == @arr_2; + + # more validation tests should be done...I'll leave it to you :-) + + my $magic_number; + + my %differences; + + OUTER: for my $i (0 .. $#arr_1) { + + for my $y (0 .. $#arr_2) { + + # calculate the difference between elements + of the two arrays + + my $diff = $arr_2[$y] - $arr_1[$i]; + + # if there is a magic number, then its frequency + must equal the size of @arr_1 (= length @arr_2) + + if (++$differences{$diff} == scalar(@arr_1)) { + $magic_number = $diff; + last OUTER; + } + } + } + + if (defined $magic_number) { + print "The magic number is: $magic_number\n"; + } else { + print "No magic number found\n"; + } +} + + +# TESTS + +my (@x, @y); + +# Example 1 +@x = (3, 7, 5); +@y = (9, 5, 7); +magic_number(\@x, \@y); # Output: The magic number is: 2 +magic_number(\@y, \@x); # Output: The magic number is: -2 + +# Example 2 +@x = (1, 2, 1); +@y = (5, 4, 4); +magic_number(\@x, \@y); # Output: The magic number is: 3 +magic_number(\@y, \@x); # Output: The magic number is: -3 + +# Example 3 +@x = (2); +@y = (5); +magic_number(\@x, \@y); # Output: The magic number is: 3 +magic_number(\@y, \@x); # Output: The magic number is: -3 + +# Example 4 +@x = (2, 3, 4); +@y = (5, 7, 9); +magic_number(\@x, \@y); # Output: No magic number found +magic_number(\@y, \@x); # Output: No magic number found diff --git a/challenge-268/reinier-maliepaard/perl/ch-2.pl b/challenge-268/reinier-maliepaard/perl/ch-2.pl new file mode 100644 index 0000000000..6ee2cc5f85 --- /dev/null +++ b/challenge-268/reinier-maliepaard/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use List::MoreUtils qw(natatime); + +sub number_game { + + my @ints = sort (@_); + my @new_arr = (); + + # https://metacpan.org/pod/List::MoreUtils -> natatime + # natatime creates an array iterator, for looping over an array in chunks of $n items at a time. + # in our case $n = 2 + my $it = (natatime 2, @ints); + while (my @vals = reverse( $it->() )) { + push(@new_arr, @vals); + } + print "(", join(", ", @new_arr), ")\n"; + +} + +# TESTS + +my @ints; + +# Example 1 +@ints = (2, 5, 3, 4); +number_game(@ints); # Output: (3, 2, 5, 4) + +# Example 2 +@ints = (9, 4, 1, 3, 6, 4, 6, 1); +number_game(@ints); # Output: (1, 1, 4, 3, 6, 4, 9, 6) + +# Example 3 +@ints = (1, 2, 2, 3); +number_game(@ints); # Output: (2, 1, 3, 2) diff --git a/stats/pwc-current.json b/stats/pwc-current.json index efca114945..73c4c06832 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,136 +1,4 @@ { - "series" : [ - { - "data" : [ - { - "y" : 3, - "name" : "Ali Moradi", - "drilldown" : "Ali Moradi" - }, - { - "drilldown" : "Andrew Shitov", - "name" : "Andrew Shitov", - "y" : 2 - }, - { - "y" : 4, - "drilldown" : "Athanasius", - "name" : "Athanasius" - }, - { - "drilldown" : "Bob Lied", - "name" : "Bob Lied", - "y" : 3 - }, - { - "y" : 2, - "name" : "Dave Jacoby", - "drilldown" : "Dave Jacoby" - }, - { - "drilldown" : "David Ferrone", - "name" : "David Ferrone", - "y" : 2 - }, - { - "y" : 2, - "drilldown" : "E. Choroba", - "name" : "E. Choroba" - }, - { - "y" : 2, - "drilldown" : "Feng Chang", - "name" : "Feng Chang" - }, - { - "name" : "Mark Anderson", - "drilldown" : "Mark Anderson", - "y" : 2 - }, - { - "drilldown" : "Matthew Neleigh", - "name" : "Matthew Neleigh", - "y" : 2 - }, - { - "y" : 2, - "name" : "Niels van Dijke", - "drilldown" : "Niels van Dijke" - }, - { - "y" : 5, - "name" : "Packy Anderson", - "drilldown" : "Packy Anderson" - }, - { - "y" : 3, - "name" : "Peter Campbell Smith", - "drilldown" : "Peter Campbell Smith" - }, - { - "name" : "Peter Meszaros", - "drilldown" : "Peter Meszaros", - "y" : 2 - }, - { - "name" : "Roger Bell_West", - "drilldown" : "Roger Bell_West", - "y" : 4 - }, - { - "y" : 4, - "name" : "Thomas Kohler", - "drilldown" : "Thomas Kohler" - }, - { - "y" : 4, - "name" : "Ulrich Rieke", - "drilldown" : "Ulrich Rieke" - }, - { - "drilldown" : "W. Luis Mochan", - "name" : "W. Luis Mochan", - "y" : 3 - } - ], - "name" : "The Weekly Challenge - 268", - "colorByPoint" : 1 - } - ], - "subtitle" : { - "text" : "[Champions: 18] Last updated at 2024-05-08 08:11:16 GMT" - }, - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - } - } - }, - "tooltip" : { - "followPointer" : 1, - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" - }, - "legend" : { - "enabled" : 0 - }, - "xAxis" : { - "type" : "category" - }, - "chart" : { - "type" : "column" - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "title" : { - "text" : "The Weekly Challenge - 268" - }, "drilldown" : { "series" : [ { @@ -144,8 +12,8 @@ 1 ] ], - "id" : "Ali Moradi", - "name" : "Ali Moradi" + "name" : "Ali Moradi", + "id" : "Ali Moradi" }, { "data" : [ @@ -154,12 +22,22 @@ 2 ] ], - "id" : "Andrew Shitov", - "name" : "Andrew Shitov" + "name" : "Andrew Shitov", + "id" : "Andrew Shitov" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Asher Harvey-Smith", + "id" : "Asher Harvey-Smith" }, { - "name" : "Athanasius", "id" : "Athanasius", + "name" : "Athanasius", "data" : [ [ "Perl", @@ -172,8 +50,8 @@ ] }, { - "name" : "Bob Lied", "id" : "Bob Lied", + "name" : "Bob Lied", "data" : [ [ "Perl", @@ -186,34 +64,34 @@ ] }, { + "id" : "Dave Jacoby", + "name" : "Dave Jacoby", "data" : [ [ "Perl", 2 ] - ], - "id" : "Dave Jacoby", - "name" : "Dave Jacoby" + ] }, { + "id" : "David Ferrone", + "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] - ], - "id" : "David Ferrone", - "name" : "David Ferrone" + ] }, { + "id" : "E. Choroba", + "name" : "E. Choroba", "data" : [ [ "Perl", 2 ] - ], - "id" : "E. Choroba", - "name" : "E. Choroba" + ] }, { "id" : "Feng Chang", @@ -226,8 +104,8 @@ ] }, { - "name" : "Mark Anderson", - "id" : "Mark Anderson", + "name" : "Jan Krnavek", + "id" : "Jan Krnavek", "data" : [ [ "Raku", @@ -236,12 +114,58 @@ ] }, { - "name" : "Matthew Neleigh", - "id" : "Matthew Neleigh", + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey", "data" : [ [ "Perl", 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Laurent Rosenfeld", + "id" : "Laurent Rosenfeld", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 2 + ] + ] + }, + { + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 9 + ] + ], + "name" : "Luca Ferrari", + "id" : "Luca Ferrari" + }, + { + "id" : "Mark Anderson", + "name" : "Mark Anderson", + "data" : [ + [ + "Raku", + 2 ] ] }, @@ -252,12 +176,30 @@ 2 ] ], + "name" : "Matthew Neleigh", + "id" : "Matthew Neleigh" + }, + { + "id" : "Nelo Tovar", + "name" : "Nelo Tovar", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "Niels van Dijke", "id" : "Niels van Dijke", - "name" : "Niels van Dijke" + "data" : [ + [ + "Perl", + 2 + ] + ] }, { - "name" : "Packy Anderson", - "id" : "Packy Anderson", "data" : [ [ "Perl", @@ -271,11 +213,13 @@ "Blog", 1 ] - ] + ], + "id" : "Packy Anderson", + "name" : "Packy Anderson" }, { - "name" : "Peter Campbell Smith", "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith", "data" : [ [ "Perl", @@ -294,24 +238,54 @@ 2 ] ], - "name" : "Peter Meszaros", - "id" : "Peter Meszaros" + "id" : "Peter Meszaros", + "name" : "Peter Meszaros" }, { + "id" : "Reinier Maliepaard", + "name" : "Reinier Maliepaard", "data" : [ [ "Perl", 2 ], [ - "Raku", + "Blog", + 1 + ] + ] + }, + { + "id" : "Robbie Hatley", + "name" : "Robbie Hatley", + "data" : [ + [ + "Perl", 2 + ], + [ + "Blog", + 1 ] - ], + ] + }, + { + "id" : "Roger Bell_West", "name" : "Roger Bell_West", - "id" : "Roger Bell_West" + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ] }, { + "id" : "Thomas Kohler", + "name" : "Thomas Kohler", "data" : [ [ "Perl", @@ -321,13 +295,9 @@ "Blog", 2 ] - ], - "id" : "Thomas Kohler", - "name" : "Thomas Kohler" + ] }, { - "id" : "Ulrich Rieke", - "name" : "Ulrich Rieke", "data" : [ [ "Perl", @@ -337,9 +307,13 @@ "Raku", 2 ] - ] + ], + "name" : "Ulrich Rieke", + "id" : "Ulrich Rieke" }, { + "name" : "W. Luis Mochan", + "id" : "W. Luis Mochan", "data" : [ [ "Perl", @@ -349,10 +323,180 @@ "Blog", 1 ] - ], - "id" : "W. Luis Mochan", - "name" : "W. Luis Mochan" + ] } ] + }, + "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/>" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "subtitle" : { + "text" : "[Champions: 26] Last updated at 2024-05-11 13:34:18 GMT" + }, + "xAxis" : { + "type" : "category" + }, + "chart" : { + "type" : "column" + }, + "legend" : { + "enabled" : 0 + }, + "series" : [ + { + "data" : [ + { + "drilldown" : "Ali Moradi", + "name" : "Ali Moradi", + "y" : 3 + }, + { + "y" : 2, + "name" : "Andrew Shitov", + "drilldown" : "Andrew Shitov" + }, + { + "drilldown" : "Asher Harvey-Smith", + "name" : "Asher Harvey-Smith", + "y" : 2 + }, + { + "y" : 4, + "name" : "Athanasius", + "drilldown" : "Athanasius" + }, + { + "drilldown" : "Bob Lied", + "name" : "Bob Lied", + "y" : 3 + }, + { + "y" : 2, + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby" + }, + { + "drilldown" : "David Ferrone", + "y" : 2, + "name" : "David Ferrone" + }, + { + "drilldown" : "E. Choroba", + "name" : "E. Choroba", + "y" : 2 + }, + { + "y" : 2, + "name" : "Feng Chang", + "drilldown" : "Feng Chang" + }, + { + "y" : 2, + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek" + }, + { + "drilldown" : "Jorg Sommrey", + "y" : 3, + "name" : "Jorg Sommrey" + }, + { + "y" : 6, + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld" + }, + { + "name" : "Luca Ferrari", + "y" : 11, + "drilldown" : "Luca Ferrari" + }, + { + "drilldown" : "Mark Anderson", + "y" : 2, + "name" : "Mark Anderson" + }, + { + "name" : "Matthew Neleigh", + "y" : 2, + "drilldown" : "Matthew Neleigh" + }, + { + "drilldown" : "Nelo Tovar", + "name" : "Nelo Tovar", + "y" : 2 + }, + { + "name" : "Niels van Dijke", + "y" : 2, + "drilldown" : "Niels van Dijke" + }, + { + "y" : 5, + "name" : "Packy Anderson", + "drilldown" : "Packy Anderson" + }, + { + "drilldown" : "Peter Campbell Smith", + "y" : 3, + "name" : "Peter Campbell Smith" + }, + { + "name" : "Peter Meszaros", + "y" : 2, + "drilldown" : "Peter Meszaros" + }, + { + "drilldown" : "Reinier Maliepaard", + "y" : 3, + "name" : "Reinier Maliepaard" + }, + { + "drilldown" : "Robbie Hatley", + "name" : "Robbie Hatley", + "y" : 3 + }, + { + "y" : 4, + "name" : "Roger Bell_West", + "drilldown" : "Roger Bell_West" + }, + { + "y" : 4, + "name" : "Thomas Kohler", + "drilldown" : "Thomas Kohler" + }, + { + "drilldown" : "Ulrich Rieke", + "name" : "Ulrich Rieke", + "y" : 4 + }, + { + "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan", + "y" : 3 + } + ], + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 268" + } + ], + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }, + "title" : { + "text" : "The Weekly Challenge - 268" } } diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index 5f8c72a434..11d53cbccb 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -1,63 +1,63 @@ { - "title" : { - "text" : "The Weekly Challenge Contributions [2019 - 2024]" - }, - "xAxis" : { - "labels" : { - "style" : { - "fontSize" : "13px", - "fontFamily" : "Verdana, sans-serif" - } - }, - "type" : "category" - }, - "legend" : { - "enabled" : "false" - }, - "yAxis" : { - "title" : { - "text" : null - }, - "min" : 0 - }, "chart" : { "type" : "column" }, - "subtitle" : { - "text" : "Last updated at 2024-05-08 08:11:16 GMT" - }, - "tooltip" : { - "pointFormat" : "<b>{point.y:.0f}</b>" + "legend" : { + "enabled" : "false" }, "series" : [ { - "dataLabels" : { - "style" : { - "fontFamily" : "Verdana, sans-serif", - "fontSize" : "13px" - }, - "color" : "#FFFFFF", - "align" : "right", - "y" : 10, - "format" : "{point.y:.0f}", - "enabled" : "true", - "rotation" : -90 - }, + "name" : "Contributions", "data" : [ [ "Blog", - 4823 + 4837 ], [ "Perl", - 13893 + 13903 ], [ "Raku", - 8051 + 8059 ] ], - |
