diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-10-02 21:31:25 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-10-02 21:31:25 +0100 |
| commit | 3898d4606c38eec2b06a994a139e82446dd0ef56 (patch) | |
| tree | bd4da7b0fa6b79b2ffe734d8bf4ee2adffa9fa7e | |
| parent | 7a924ade9ee9bd90a8d1ae1188370f2a48ef8baf (diff) | |
| download | perlweeklychallenge-club-3898d4606c38eec2b06a994a139e82446dd0ef56.tar.gz perlweeklychallenge-club-3898d4606c38eec2b06a994a139e82446dd0ef56.tar.bz2 perlweeklychallenge-club-3898d4606c38eec2b06a994a139e82446dd0ef56.zip | |
- Added solutions by Mark Anderson.
- Added solutions by Jaldhar H. Vyas.
- Added solutions by Dave Jacoby.
- Added solutions by E. Choroba.
- Added solutions by PokGoPun.
- Added solutions by Tim King.
- Added solutions by Peter Meszaros.
- Added solutions by Paulo Custodio.
- Added solutions by Peter Pentchev.
- Added solutions by Reinier Maliepaard.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Vinod Kumar K.
30 files changed, 3650 insertions, 3299 deletions
diff --git a/challenge-289/laurent-rosenfeld/blog.txt b/challenge-289/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..78e3760912 --- /dev/null +++ b/challenge-289/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/10/perl-weekly-challenge-289-third-maximum.html diff --git a/challenge-289/laurent-rosenfeld/perl/ch-1.pl b/challenge-289/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..6cd1457047 --- /dev/null +++ b/challenge-289/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; +use feature 'say'; + +sub third_max { + my %unique = map { $_ => 1 } @_; + my @sorted = sort { $b <=> $a } keys %unique; + return exists $sorted[2] ? $sorted[2] : $sorted[0]; +} + +my @tests = ( [5, 6, 4, 1], [4, 5], [1, 2, 2, 3] ); +for my $test (@tests) { + printf "%-10s => ", "@$test"; + say third_max @$test; +} diff --git a/challenge-289/laurent-rosenfeld/raku/ch-1.raku b/challenge-289/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..753fb1642b --- /dev/null +++ b/challenge-289/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,10 @@ +sub third-max (@in) { + my @sorted = @in.unique.sort.reverse; + return @sorted[2]:exists ?? @sorted[2] !! @sorted[0]; +} + +my @tests = (5, 6, 4, 1), (4, 5), (1, 2, 2, 3); +for @tests -> @test { + printf "%-10s => ", "@test[]"; + say third-max @test; +} diff --git a/challenge-289/reinier-maliepaard/blog.txt b/challenge-289/reinier-maliepaard/blog.txt new file mode 100644 index 0000000000..e74462fb1f --- /dev/null +++ b/challenge-289/reinier-maliepaard/blog.txt @@ -0,0 +1 @@ +https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc289 diff --git a/challenge-289/reinier-maliepaard/perl/ch-1.pl b/challenge-289/reinier-maliepaard/perl/ch-1.pl new file mode 100644 index 0000000000..9890d4699d --- /dev/null +++ b/challenge-289/reinier-maliepaard/perl/ch-1.pl @@ -0,0 +1,115 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub third_maximum { + + # remove duplicates: see example 3 and 4 + my @unique = keys %{{ map { $_ => 1 } @_ }}; + + # sort numerically + @unique = sort { $a <=> $b } @unique; + + # return the third largest or the largest if fewer than 3 elements + return (scalar(@unique) >= 3) ? $unique[-3] : $unique[-1]; +} + +# Tests +my @ints; + +Example 1 +@ints = (5, 6, 4, 1); +print(third_maximum(@ints), "\n"); # Output 4 + +Example 2 +@ints = (4, 5); +print(third_maximum(@ints), "\n"); # Output: 5 + +Example 3 +@ints = (1, 2, 2, 3); +print(third_maximum(@ints), "\n"); # Output: 1 + +Example 4 +@ints = (3, 3, 3, 3, 2, 2, 2, 1); +print(third_maximum(@ints), "\n"); # Output: 1 + + +TASK #2: Jumbled Letters +An Internet legend dating back to at least 2001 goes something like this: + +Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer be at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe. + +This supposed Cambridge research is unfortunately an urban legend. However, the effect has been studied. For example—and with a title that probably made the journal’s editor a little nervous—Raeding wrods with jubmled lettres: there is a cost by Rayner, White, et. al. looked at reading speed and comprehension of jumbled text. + +Your task is to write a program that takes English text as its input and outputs a jumbled version as follows: + + The first and last letter of every word must stay the same + The remaining letters in the word are scrambled in a random order (if that happens to be the original order, that is OK). + Whitespace, punctuation, and capitalization must stay the same + The order of words does not change, only the letters inside the word + +So, for example, "Perl" could become "Prel", or stay as "Perl", but it could not become "Pelr" or "lreP". + +#!/usr/bin/perl +use strict; +use warnings; + +sub shuffle_words { + my $text = shift; + # use a regular expression to match and shuffle each word + $text =~ s/(\w+)([^\w]*)/shuffle_word($1). $2/ge; + return $text; +} + +sub shuffle_word { + + my $word = shift; + # if the word has 2 or fewer characters, return it unchanged + return ($word) if (length($word) <= 2); + + # call the fisher_yates_shuffle subroutine to shuffle the middle characters + my $first_char = substr($word, 0, 1); + my $middle = substr($word, 1, length($word) - 2); + my $last_char = substr($word, -1); + return $first_char . fisher_yates_shuffle($middle) . $last_char; +} + +# define a subroutine to shuffle an array using the Fisher-Yates algorithm +sub fisher_yates_shuffle { + + my @chars = split(//, $_[0]); + + my $i = scalar (@chars); + # iterate over the array, swapping each element with a random element + while ($i--) { + # generate a random index + my $j = int rand($i + 1); + # swap the current element with the element at the random index + @chars[$i, $j] = @chars[$j, $i]; + } + + return (join('', @chars)); +} + +Tests + +my $str; + +Example 1 +$str = "this is, this IS an exCellent Idea!!"; + +$str = shuffle_words($str); +print("$str\n"); # e.g. this is, tihs IS an eelelCxnt Ieda!! + +Example 2 +$str = "The Fisher-Yates shuffle is an efficient algorithm for producing +a random permutation of a finite sequence by repeatedly selecting and +swapping elements in place, ensuring that every possible arrangement is +equally likely."; + +$str = shuffle_words($str); +print("$str\n"); # e.g. The Fsiehr-Yteas sfflhue is an encieffit alighotrm for prniucdog +a raodnm pimatoerutn of a fnitie sncqueee by relpeaetdy sienetlcg and +sniappwg etlmeens in palce, einnrsug taht ervey pssbiole aranrgenemt is +ellaquy likley. + diff --git a/challenge-289/vinod-k/perl/ch-1.perl b/challenge-289/vinod-k/perl/ch-1.pl index 7ae5738224..7ae5738224 100644 --- a/challenge-289/vinod-k/perl/ch-1.perl +++ b/challenge-289/vinod-k/perl/ch-1.pl diff --git a/stats/pwc-challenge-287.json b/stats/pwc-challenge-287.json index ce4eee1b19..d0a17ea7aa 100644 --- a/stats/pwc-challenge-287.json +++ b/stats/pwc-challenge-287.json @@ -2,27 +2,13 @@ "title" : { "text" : "The Weekly Challenge - 287" }, - "tooltip" : { - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", - "followPointer" : 1 - }, - "legend" : { - "enabled" : 0 - }, - "subtitle" : { - "text" : "[Champions: 33] Last updated at 2024-09-28 09:51:17 GMT" - }, "yAxis" : { "title" : { "text" : "Total Solutions" } }, - "xAxis" : { - "type" : "category" - }, - "chart" : { - "type" : "column" + "legend" : { + "enabled" : 0 }, "drilldown" : { "series" : [ @@ -69,14 +55,14 @@ "name" : "Arne Sommer" }, { + "name" : "BarrOff", + "id" : "BarrOff", "data" : [ [ "Raku", 1 ] - ], - "id" : "BarrOff", - "name" : "BarrOff" + ] }, { "name" : "Bob Lied", @@ -93,14 +79,14 @@ ] }, { - "id" : "David Ferrone", - "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "David Ferrone", + "name" : "David Ferrone" }, { "name" : "E. Choroba", @@ -117,26 +103,46 @@ ] }, { - "name" : "Feng Chang", + "data" : [ + [ + "Raku", + 2 + ] + ], "id" : "Feng Chang", + "name" : "Feng Chang" + }, + { + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas", "data" : [ [ + "Perl", + 2 + ], + [ "Raku", 2 + ], + [ + "Blog", + 1 ] ] }, { - "id" : "Jan Krnavek", - "name" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Jan Krnavek", + "id" : "Jan Krnavek" }, { + "name" : "Jorg Sommrey", + "id" : "Jorg Sommrey", "data" : [ [ "Perl", @@ -146,19 +152,17 @@ "Blog", 1 ] - ], - "id" : "Jorg Sommrey", - "name" : "Jorg Sommrey" + ] }, { + "id" : "Kjetil Skotheim", + "name" : "Kjetil Skotheim", "data" : [ [ "Perl", 2 ] - ], - "id" : "Kjetil Skotheim", - "name" : "Kjetil Skotheim" + ] }, { "id" : "Laurent Rosenfeld", @@ -189,8 +193,8 @@ ] }, { - "name" : "Luca Ferrari", "id" : "Luca Ferrari", + "name" : "Luca Ferrari", "data" : [ [ "Raku", @@ -203,8 +207,8 @@ ] }, { - "id" : "Mark Anderson", "name" : "Mark Anderson", + "id" : "Mark Anderson", "data" : [ [ "Raku", @@ -213,14 +217,14 @@ ] }, { + "id" : "Matthew Neleigh", + "name" : "Matthew Neleigh", "data" : [ [ "Perl", 2 ] - ], - "id" : "Matthew Neleigh", - "name" : "Matthew Neleigh" + ] }, { "data" : [ @@ -237,18 +241,16 @@ "name" : "Matthias Muth" }, { - "id" : "Niels van Dijke", - "name" : "Niels van Dijke", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Niels van Dijke", + "name" : "Niels van Dijke" }, { - "name" : "Packy Anderson", - "id" : "Packy Anderson", "data" : [ [ "Perl", @@ -262,17 +264,19 @@ "Blog", 1 ] - ] + ], + "name" : "Packy Anderson", + "id" : "Packy Anderson" }, { + "id" : "Paulo Custodio", + "name" : "Paulo Custodio", "data" : [ [ "Perl", 2 ] - ], - "name" : "Paulo Custodio", - "id" : "Paulo Custodio" + ] }, { "data" : [ @@ -285,20 +289,22 @@ 1 ] ], - "name" : "Peter Campbell Smith", - "id" : "Peter Campbell Smith" + "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" }, { + "id" : "Peter Meszaros", + "name" : "Peter Meszaros", "data" : [ [ "Perl", 2 ] - ], - "name" : "Peter Meszaros", - "id" : "Peter Meszaros" + ] }, { + "name" : "Peter Pentchev", + "id" : "Peter Pentchev", "data" : [ [ "Perl", @@ -312,11 +318,11 @@ "Blog", 1 ] - ], - "name" : "Peter Pentchev", - "id" : "Peter Pentchev" + ] }, { + "id" : "Reinier Maliepaard", + "name" : "Reinier Maliepaard", "data" : [ [ "Perl", @@ -326,13 +332,9 @@ "Blog", 1 ] - ], - "id" : "Reinier Maliepaard", - "name" : "Reinier Maliepaard" + ] }, { - "name" : "Robbie Hatley", - "id" : "Robbie Hatley", "data" : [ [ "Perl", @@ -342,21 +344,21 @@ "Blog", 1 ] - ] + ], + "name" : "Robbie Hatley", + "id" : "Robbie Hatley" }, { + "name" : "Robert Ransbottom", + "id" : "Robert Ransbottom", "data" : [ [ "Raku", 2 ] - ], - "name" : "Robert Ransbottom", - "id" : "Robert Ransbottom" + ] }, { - "id" : "Roger Bell_West", - "name" : "Roger Bell_West", "data" : [ [ "Perl", @@ -370,11 +372,13 @@ "Blog", 1 ] - ] + ], + "id" : "Roger Bell_West", + "name" : "Roger Bell_West" }, { - "name" : "Santiago Leyva", "id" : "Santiago Leyva", + "name" : "Santiago Leyva", "data" : [ [ "Perl", @@ -393,22 +397,20 @@ 1 ] ], - "id" : "Simon Green", - "name" : "Simon Green" + "name" : "Simon Green", + "id" : "Simon Green" }, { - "name" : "Tim King", - "id" : "Tim King", "data" : [ [ "Perl", 1 ] - ] + ], + "id" : "Tim King", + "name" : "Tim King" }, { - "id" : "Ulrich Rieke", - "name" : "Ulrich Rieke", "data" : [ [ "Perl", @@ -418,11 +420,11 @@ "Raku", 2 ] - ] + ], + "id" : "Ulrich Rieke", + "name" : "Ulrich Rieke" }, { - "id" : "W. Luis Mochan", - "name" : "W. Luis Mochan", "data" : [ [ "Perl", @@ -432,7 +434,9 @@ "Blog", 1 ] - ] + ], + "name" : "W. Luis Mochan", + "id" : "W. Luis Mochan" }, { "data" : [ @@ -441,11 +445,22 @@ 2 ] ], - "id" : "Wanderdoc", - "name" : "Wanderdoc" + "name" : "Wanderdoc", + "id" : "Wanderdoc" } ] }, + "chart" : { + "type" : "column" + }, + "xAxis" : { + "type" : "category" + }, + "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/>" + }, "plotOptions" : { "series" : { "dataLabels" : { @@ -457,16 +472,16 @@ }, "series" : [ { - "colorByPoint" : 1, + "name" : "The Weekly Challenge - 287", "data" : [ { + "drilldown" : "Andre Ploger", "name" : "Andre Ploger", - "y" : 3, - "drilldown" : "Andre Ploger" + "y" : 3 }, { - "name" : "Andrew Schneider", "drilldown" : "Andrew Schneider", + "name" : "Andrew Schneider", "y" : 3 }, { @@ -475,33 +490,38 @@ "name" : "Arne Sommer" }, { - "drilldown" : "BarrOff", + "name" : "BarrOff", "y" : 1, - "name" : "BarrOff" + "drilldown" : "BarrOff" }, { - "name" : "Bob Lied", "y" : 3, + "name" : "Bob Lied", "drilldown" : "Bob Lied" }, { - "name" : "David Ferrone", "drilldown" : "David Ferrone", - "y" : 2 + "y" : 2, + "name" : "David Ferrone" }, { - "drilldown" : "E. Choroba", + "name" : "E. Choroba", "y" : 3, - "name" : "E. Choroba" + "drilldown" : "E. Choroba" }, { "name" : "Feng Chang", - "drilldown" : "Feng Chang", - "y" : 2 + "y" : 2, + "drilldown" : "Feng Chang" + }, + { + "drilldown" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas", + "y" : 5 }, { - "name" : "Jan Krnavek", "drilldown" : "Jan Krnavek", + "name" : "Jan Krnavek", "y" : 2 }, { @@ -510,9 +530,9 @@ "name" : "Jorg Sommrey" }, { - "drilldown" : "Kjetil Skotheim", + "name" : "Kjetil Skotheim", "y" : 2, - "name" : "Kjetil Skotheim" + "drilldown" : "Kjetil Skotheim" }, { "name" : "Laurent Rosenfeld", @@ -521,48 +541,48 @@ }, { "name" : "Lubos Kolouch", - "drilldown" : "Lubos Kolouch", - "y" : 2 + "y" : 2, + "drilldown" : "Lubos Kolouch" }, { "y" : 12, - "drilldown" : "Luca Ferrari", - "name" : "Luca Ferrari" + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari" }, { - "y" : 2, "drilldown" : "Mark Anderson", - "name" : "Mark Anderson" + "name" : "Mark Anderson", + "y" : 2 }, { - "name" : "Matthew Neleigh", "drilldown" : "Matthew Neleigh", + "name" : "Matthew Neleigh", "y" : 2 }, { + "name" : "Matthias Muth", "y" : 3, - "drilldown" : "Matthias Muth", - "name" : "Matthias Muth" + "drilldown" : "Matthias Muth" }, { + "drilldown" : "Niels van Dijke", "name" : "Niels van Dijke", - "y" : 2, - "drilldown" : "Niels van Dijke" + "y" : 2 }, { - "name" : "Packy Anderson", "y" : 5, + "name" : "Packy Anderson", "drilldown" : "Packy Anderson" }, { - "drilldown" : "Paulo Custodio", "y" : 2, - "name" : "Paulo Custodio" + "name" : "Paulo Custodio", + "drilldown" : "Paulo Custodio" }, { "name" : "Peter Campbell Smith", - "drilldown" : "Peter Campbell Smith", - "y" : 3 + "y" : 3, + "drilldown" : "Peter Campbell Smith" }, { "name" : "Peter Meszaros", @@ -570,14 +590,14 @@ "drilldown" : "Peter Meszaros" }, { - "name" : "Peter Pentchev", "y" : 5, + "name" : "Peter Pentchev", "drilldown" : "Peter Pentchev" }, { - "name" : "Reinier Maliepaard", "drilldown" : "Reinier Maliepaard", - "y" : 2 + "y" : 2, + "name" : "Reinier Maliepaard" }, { "name" : "Robbie Hatley", @@ -585,47 +605,50 @@ "drilldown" : "Robbie Hatley" }, { - "name" : "Robert Ransbottom", "drilldown" : "Robert Ransbottom", + "name" : "Robert Ransbottom", "y" : 2 }, { - "y" : 5, "drilldown" : "Roger Bell_West", + "y" : 5, "name" : "Roger Bell_West" }, { - "name" : "Santiago Leyva", "y" : 2, + "name" : "Santiago Leyva", "drilldown" : "Santiago Leyva" }, { - "drilldown" : "Simon Green", "y" : 3, - "name" : "Simon Green" + "name" : "Simon Green", + "drilldown" : "Simon Green" }, { + "y" : 1, "name" : "Tim King", - "drilldown" : "Tim King", - "y" : 1 + "drilldown" : "Tim King" }, { + "name" : "Ulrich Rieke", "y" : 4, - "drilldown" : "Ulrich Rieke", - "name" : "Ulrich Rieke" + "drilldown" : "Ulrich Rieke" }, { - "name" : "W. Luis Mochan", "y" : 3, + "name" : "W. Luis Mochan", "drilldown" : "W. Luis Mochan" }, { "name" : "Wanderdoc", - "drilldown" : "Wanderdoc", - "y" : 2 + "y" : 2, + "drilldown" : "Wanderdoc" } ], - "name" : "The Weekly Challenge - 287" + "colorByPoint" : 1 } - ] + ], + "subtitle" : { + "text" : "[Champions: 34] Last updated at 2024-10-02 20:30:44 GMT" + } } diff --git a/stats/pwc-challenge-288.json b/stats/pwc-challenge-288.json index 5535887ddf..eca0d93093 100644 --- a/stats/pwc-challenge-288.json +++ b/stats/pwc-challenge-288.json @@ -1,10 +1,185 @@ { + "xAxis" : { + "type" : "category" + }, + "tooltip" : { + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", + "followPointer" : 1, + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + }, + "borderWidth" : 0 + } + }, + "series" : [ + { + "data" : [ + { + "drilldown" : "Andre Ploger", + "name" : "Andre Ploger", + "y" : 3 + }, + { + "y" : 3, + "name" : "Arne Sommer", + "drilldown" : "Arne Sommer" + }, + { + "name" : "Athanasius", + "y" : 4, + "drilldown" : "Athanasius" + }, + { + "y" : 1, + "name" : "BarrOff", + "drilldown" : "BarrOff" + }, + { + "y" : 2, + "name" : "Bob Lied", + "drilldown" : "Bob Lied" + }, + { + "y" : 2, + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby" + }, + { |
