diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-07-09 17:53:40 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-07-09 17:53:40 +0100 |
| commit | 96580cd62594876fa534d57eb553637433c8ba04 (patch) | |
| tree | ecfea2f0f01a37261e5495d262f76ce88296389c | |
| parent | d4cb625b21df7b106a6b2272049a5c9842e186ad (diff) | |
| download | perlweeklychallenge-club-96580cd62594876fa534d57eb553637433c8ba04.tar.gz perlweeklychallenge-club-96580cd62594876fa534d57eb553637433c8ba04.tar.bz2 perlweeklychallenge-club-96580cd62594876fa534d57eb553637433c8ba04.zip | |
- Added solutions by Peter Meszaros.
- Added solutions by Steven Wilson.
- Added solutions by Matthew Neleigh.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Reinier Maliepaard.
23 files changed, 3236 insertions, 3018 deletions
diff --git a/challenge-277/laurent-rosenfeld/blog.txt b/challenge-277/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..c5f83d45db --- /dev/null +++ b/challenge-277/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/07/perl-weekly-challenge-277-count-common.html diff --git a/challenge-277/laurent-rosenfeld/perl/ch-1.pl b/challenge-277/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..3c1137e0df --- /dev/null +++ b/challenge-277/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,35 @@ +use warnings; +use feature 'say'; +use Data::Dumper; + +sub count_common { + my (%in1, %in2); + for my $word (@{$_[0]}) { + $in1{$word}++; + } + for my $word (@{$_[1]}) { + $in2{$word}++; + } + my %unique1 = map { $_ => 1 } grep {$in1{$_} == 1} keys %in1; + my %unique2 = map { $_ => 1 } grep {$in2{$_} == 1} keys %in2; + my @intersect; + for my $word (keys %unique1) { + push @intersect, $word if exists $unique2{$word}; + } + return scalar @intersect; +} +my @tests = ( [ [<Perl is my friend>], + [<Perl and Raku are friend>] ], + [ [<Perl is my friend>], + [<Raku is friend of my friend Perl>] ], + [ [<Perl and Python are very similar>], + [<Python is top in guest languages>] ], + [ [<Perl is imperative Lisp is functional>], + [<Crystal is similar to Ruby>] ] + ); +for my $test (@tests) { + say "@{$test->[0]}"; + say "@{$test->[1]}"; + say count_common $test->[0], $test->[1]; + say ""; +} diff --git a/challenge-277/laurent-rosenfeld/raku/ch-1.raku b/challenge-277/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..32ba7f01f7 --- /dev/null +++ b/challenge-277/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,23 @@ +sub count-common (@in1, @in2) { + my $bag-in1 = @in1.Bag; + my $bag-in2 = @in2.Bag; + my $unique1 = grep {$bag-in1{$_} == 1}, $bag-in1.keys; . + my $unique2 = grep {$bag-in2{$_} == 1}, $bag-in2.keys; + return ($unique1 ∩ $unique2).elems; +} + +my @tests = ( <Perl is my friend>, + <Perl and Raku are friend> ), + ( <Perl is my friend>, + <Raku is friend of my friend Perl> ), + ( <Perl and Python are very similar>, + <Python is top in guest languages> ), + ( <Perl is imperative Lisp is functional>, + <Crystal is similar to Ruby> ); + +for @tests -> @test { + say @test[0]; + say @test[1]; + say count-common @test[0], @test[1]; + say ""; +} diff --git a/challenge-277/reinier-maliepaard/blog.txt b/challenge-277/reinier-maliepaard/blog.txt new file mode 100644 index 0000000000..0a4f5cb998 --- /dev/null +++ b/challenge-277/reinier-maliepaard/blog.txt @@ -0,0 +1 @@ +https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc277 diff --git a/challenge-277/reinier-maliepaard/perl/ch-1.pl b/challenge-277/reinier-maliepaard/perl/ch-1.pl new file mode 100644 index 0000000000..46851c9126 --- /dev/null +++ b/challenge-277/reinier-maliepaard/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use List::Compare; + +sub count_common { + + my ($words1, $words2) = @_; + + # Find common elements using List::Compare + my $lc = List::Compare->new($words1, $words2); + my @common_words = $lc->get_intersection; + + my %frequency; + + # Count frequencies of common words in both arrays + foreach my $word (@common_words) { + $frequency{$word}{words1} = scalar ( grep { $_ eq $word } @$words1 ); + $frequency{$word}{words2} = scalar ( grep { $_ eq $word } @$words2 ); + } + + # Filter words that appear exactly once in both arrays + my @filtered_words = grep { $frequency{$_}{words1} == 1 && + $frequency{$_}{words2} == 1 } @common_words; + + return(scalar @filtered_words); +} + +# TESTS + +my (@words1, @words2); + +# Example 1 +@words1 = ("Perl", "is", "my", "friend"); +@words2 = ("Perl", "and", "Raku", "are", "friend"); +print(count_common(\@words1, \@words2), "\n"); # Output: 2 + +# Example 2 +@words1 = ("Perl", "and", "Python", "are", "very", "similar"); +@words2 = ("Python", "is", "top", "in", "guest", "languages"); +print(count_common(\@words1, \@words2), "\n"); # Output: 1 + +# Example 3 +@words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional"); +@words2 = ("Crystal", "is", "similar", "to", "Ruby"); +print(count_common(\@words1, \@words2), "\n"); # Output: 0 diff --git a/challenge-277/reinier-maliepaard/perl/ch-2.pl b/challenge-277/reinier-maliepaard/perl/ch-2.pl new file mode 100644 index 0000000000..34b299344a --- /dev/null +++ b/challenge-277/reinier-maliepaard/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use List::Uniq ':all'; +use Math::Combinatorics; + +sub strong_pair { + + # Duplicate values are unnecessary and lead to duplicate pairs, + # so they should be removed: uniq(@_); + + my $c = Math::Combinatorics->new ( count => 2, data => [uniq(@_)], ); + + my $count = 0; + + while ( my @cmb = $c->next_combination ) { + + my $abs = abs($cmb[0] - $cmb[1]); + # finding min(): ($x + $y + abs($x - $y)) / 2 + # https://www.perlmonks.org/?node_id=406883 + $count++ if ( ($abs < ($cmb[0] + $cmb[1] - $abs) / 2) && ($abs > 0) ); + + } + + return ($count); +} + +# TESTS + +my @ints; + +# Example 1 +@ints = (1, 2, 3, 4, 5); +print(strong_pair(@ints), "\n"); # Output: 4 + +# Example 2 +@ints = (5, 7, 1, 7); +print(strong_pair(@ints), "\n"); # Output: 1 diff --git a/stats/pwc-challenge-276.json b/stats/pwc-challenge-276.json index 8b0e0b8b99..c9c3063e30 100644 --- a/stats/pwc-challenge-276.json +++ b/stats/pwc-challenge-276.json @@ -1,228 +1,11 @@ { - "chart" : { - "type" : "column" - }, - "xAxis" : { - "type" : "category" - }, - "series" : [ - { - "data" : [ - { - "drilldown" : "Ali Moradi", - "name" : "Ali Moradi", - "y" : 5 - }, - { - "y" : 3, - "name" : "Andrew Schneider", - "drilldown" : "Andrew Schneider" - }, - { - "name" : "Arne Sommer", - "drilldown" : "Arne Sommer", - "y" : 3 - }, - { - "y" : 4, - "name" : "Athanasius", - "drilldown" : "Athanasius" - }, - { - "y" : 2, - "name" : "BarrOff", - "drilldown" : "BarrOff" - }, - { - "drilldown" : "Bob Lied", - "name" : "Bob Lied", - "y" : 2 - }, - { - "y" : 4, - "name" : "Bruce Gray", - "drilldown" : "Bruce Gray" - }, - { - "y" : 2, - "name" : "Dave Jacoby", - "drilldown" : "Dave Jacoby" - }, - { - "drilldown" : "David Ferrone", - "name" : "David Ferrone", - "y" : 2 - }, - { - "y" : 2, - "name" : "E. Choroba", - "drilldown" : "E. Choroba" - }, - { - "drilldown" : "Feng Chang", - "name" : "Feng Chang", - "y" : 2 - }, - { - "name" : "Jaldhar H. Vyas", - "drilldown" : "Jaldhar H. Vyas", - "y" : 5 - }, - { - "y" : 2, - "name" : "Jan Krnavek", - "drilldown" : "Jan Krnavek" - }, - { - "name" : "Joelle Maslak", - "drilldown" : "Joelle Maslak", - "y" : 4 - }, - { - "drilldown" : "Jorg Sommrey", - "name" : "Jorg Sommrey", - "y" : 3 - }, - { - "drilldown" : "Kai Burgdorf", - "name" : "Kai Burgdorf", - "y" : 2 - }, - { - "drilldown" : "Kjetil Skotheim", - "name" : "Kjetil Skotheim", - "y" : 2 - }, - { - "name" : "Laurent Rosenfeld", - "drilldown" : "Laurent Rosenfeld", - "y" : 6 - }, - { - "y" : 11, - "name" : "Luca Ferrari", - "drilldown" : "Luca Ferrari" - }, - { - "drilldown" : "Mariano Ortega", - "name" : "Mariano Ortega", - "y" : 2 - }, - { - "y" : 2, - "drilldown" : "Mark Anderson", - "name" : "Mark Anderson" - }, - { - "y" : 2, - "name" : "Matthew Neleigh", - "drilldown" : "Matthew Neleigh" - }, - { - "y" : 3, - "drilldown" : "Matthias Muth", - "name" : "Matthias Muth" - }, - { - "y" : 2, - "name" : "Nelo Tovar", - "drilldown" : "Nelo Tovar" - }, - { - "drilldown" : "Niels van Dijke", - "name" : "Niels van Dijke", - "y" : 2 - }, - { - "drilldown" : "Packy Anderson", - "name" : "Packy Anderson", - "y" : 5 - }, - { - "name" : "Peter Campbell Smith", - "drilldown" : "Peter Campbell Smith", - "y" : 3 - }, - { - "y" : 3, - "drilldown" : "Reinier Maliepaard", - "name" : "Reinier Maliepaard" - }, - { - "drilldown" : "Robbie Hatley", - "name" : "Robbie Hatley", - "y" : 3 - }, - { - "y" : 2, - "name" : "Robert Ransbottom", - "drilldown" : "Robert Ransbottom" - }, - { - "y" : 5, - "name" : "Roger Bell_West", - "drilldown" : "Roger Bell_West" - }, - { - "y" : 3, - "drilldown" : "Ryan Thompson", - "name" : "Ryan Thompson" - }, - { - "name" : "Simon Green", - "drilldown" : "Simon Green", - "y" : 3 - }, - { - "name" : "Thomas Kohler", - "drilldown" : "Thomas Kohler", - "y" : 4 - }, - { - "y" : 4, - "name" : "Ulrich Rieke", - "drilldown" : "Ulrich Rieke" - }, - { - "y" : 3, - "name" : "W. Luis Mochan", - "drilldown" : "W. Luis Mochan" - }, - { - "y" : 2, - "drilldown" : "Wanderdoc", - "name" : "Wanderdoc" - } - ], - "colorByPoint" : 1, - "name" : "The Weekly Challenge - 276" - } - ], "subtitle" : { - "text" : "[Champions: 37] Last updated at 2024-07-08 13:30:43 GMT" - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "plotOptions" : { - "series" : { - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - }, - "borderWidth" : 0 - } - }, - "legend" : { - "enabled" : 0 + "text" : "[Champions: 38] Last updated at 2024-07-09 16:41:31 GMT" }, "drilldown" : { "series" : [ { "name" : "Ali Moradi", - "id" : "Ali Moradi", "data" : [ [ "Perl", @@ -236,7 +19,8 @@ "Blog", 1 ] - ] + ], + "id" : "Ali Moradi" }, { "id" : "Andrew Schneider", @@ -263,10 +47,11 @@ 1 ] ], - "id" : "Arne Sommer", - "name" : "Arne Sommer" + "name" : "Arne Sommer", + "id" : "Arne Sommer" }, { + "id" : "Athanasius", "data" : [ [ "Perl", @@ -277,12 +62,11 @@ 2 ] ], - "id" : "Athanasius", "name" : "Athanasius" }, { - "name" : "BarrOff", "id" : "BarrOff", + "name" : "BarrOff", "data" : [ [ "Raku", @@ -301,8 +85,8 @@ "id" : "Bob Lied" }, { - "name" : "Bruce Gray", "id" : "Bruce Gray", + "name" : "Bruce Gray", "data" : [ [ "Perl", @@ -315,46 +99,47 @@ ] }, { - "id" : "Dave Jacoby", + "name" : "Dave Jacoby", "data" : [ [ "Perl", 2 ] ], - "name" : "Dave Jacoby" + "id" : "Dave Jacoby" }, { - "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] ], + "name" : "David Ferrone", "id" : "David Ferrone" }, { "name" : "E. Choroba", - "id" : "E. Choroba", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "E. Choroba" }, { + "id" : "Feng Chang", "name" : "Feng Chang", "data" : [ [ "Raku", 2 ] - ], - "id" : "Feng Chang" + ] }, { + "id" : "Jaldhar H. Vyas", "name" : "Jaldhar H. Vyas", "data" : [ [ @@ -369,21 +154,19 @@ "Blog", 1 ] - ], - "id" : "Jaldhar H. Vyas" + ] }, { + "name" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] ], - "id" : "Jan Krnavek", - "name" : "Jan Krnavek" + "id" : "Jan Krnavek" }, { - "id" : "Joelle Maslak", "data" : [ [ "Perl", @@ -394,7 +177,8 @@ 2 ] ], - "name" : "Joelle Maslak" + "name" : "Joelle Maslak", + "id" : "Joelle Maslak" }, { "id" : "Jorg Sommrey", @@ -411,26 +195,27 @@ "name" : "Jorg Sommrey" }, { - "name" : "Kai Burgdorf", - "id" : "Kai Burgdorf", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Kai Burgdorf", + "id" : "Kai Burgdorf" }, { + "id" : "Kjetil Skotheim", "name" : "Kjetil Skotheim", "data" : [ [ "Perl", 2 ] - ], - "id" : "Kjetil Skotheim" + ] }, { + "id" : "Laurent Rosenfeld", "name" : "Laurent Rosenfeld", "data" : [ [ @@ -445,10 +230,11 @@ "Blog", 2 ] - ], - "id" : "Laurent Rosenfeld" + ] }, { + "id" : "Luca Ferrari", + "name" : "Luca Ferrari", "data" : [ [ "Raku", @@ -458,39 +244,37 @@ "Blog", 9 ] - ], - "id" : "Luca Ferrari", - "name" : "Luca Ferrari" + ] }, { + "name" : "Mariano Ortega", "data" : [ [ "Perl", 2 ] ], - "id" : "Mariano Ortega", - "name" : "Mariano Ortega" + "id" : "Mariano Ortega" }, { + "id" : "Mark Anderson", "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ], - "id" : "Mark Anderson" + ] }, { - "name" : "Matthew Neleigh", "id" : "Matthew Neleigh", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Matthew Neleigh" }, { "name" : "Matthias Muth", @@ -513,21 +297,22 @@ 2 ] ], - "id" : "Nelo Tovar", - "name" : "Nelo Tovar" + "name" : "Nelo Tovar", + "id" : "Nelo Tovar" }, { - "name" : "Niels van Dijke", - "id" : "Niels van Dijke", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Niels van Dijke", + "id" : "Niels van Dijke" }, { "id" : "Packy Anderson", + "name" : "Packy Anderson", "data" : [ [ "Perl", @@ -541,11 +326,9 @@ "Blog", 1 ] - ], - "name" : "Packy Anderson" + ] }, { - "id" : "Peter Campbell Smith", "data" : [ [ "Perl", @@ -556,9 +339,21 @@ 1 ] ], - "name" : "Peter Campbell Smith" + "name" : "Peter Campbell Smith", + "id" : "Peter Campbell Smith" + }, + { + "id" : "Peter Meszaros", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Peter Meszaros" }, { + "id" : "Reinier Maliepaard", "data" : [ [ "Perl", @@ -569,11 +364,9 @@ 1 ] ], - "id" : "Reinier Maliepaard", "name" : "Reinier Maliepaard" }, { - "name" : "Robbie Hatley", "data" : [ [ "Perl", @@ -584,6 +377,7 @@ 1 ] ], + "name" : "Robbie Hatley", "id" : "Robbie Hatley" }, { @@ -597,6 +391,7 @@ "id" : "Robert Ransbottom" }, { + "name" : "Roger Bell_West", "data" : [ [ "Perl", @@ -611,11 +406,9 @@ 1 ] ], - "id" : "Roger Bell_West", - "name" : "Roger Bell_West" + "id" : "Roger Bell_West" }, { - "name" : "Ryan Thompson", "data" : [ [ "Perl", @@ -626,6 +419,7 @@ 1 ] ], + "name" : "Ryan Thompson", "id" : "Ryan Thompson" }, { @@ -643,6 +437,7 @@ "id" : "Simon Green" }, { + "id" : "Thomas Kohler", "name" : "Thomas Kohler", "data" : [ [ @@ -653,12 +448,11 @@ "Blog", 2 ] - ], - "id" : "Thomas Kohler" + ] }, { - "name" : "Ulrich Rieke", "id" : "Ulrich Rieke", + "name" : "Ulrich Rieke", "data" : [ [ "Perl", @@ -681,27 +475,248 @@ 1 ] ], - "id" : "W. Luis Mochan", - "name" : "W. Luis Mochan" + "name" : "W. Luis Mochan", + "id" : "W. Luis Mochan" }, { + "id" : "Wanderdoc", "name" : "Wanderdoc", "data" : [ [ "Perl", 2 ] - ], - "id" : "Wanderdoc" + ] } ] }, - "tooltip" : { - "followPointer" : 1, - "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/>" + "xAxis" : { + "type" : "category" + }, + "legend" : { + "enabled" : 0 + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } }, "title" : { "text" : "The Weekly Challenge - 276" + }, + "chart" : { + "type" : "column" + }, + "series" : [ + { + "name" : "The Weekly Challenge - 276", + "colorByPoint" : 1, + "data" : [ + { + "name" : "Ali Moradi", + "y" : 5, + "drilldown" : "Ali Moradi" + }, + { + "y" : 3, + "name" : "Andrew Schneider", + "drilldown" : "Andrew Schneider" + }, + { + "drilldown" : "Arne Sommer", + "name" : "Arne Sommer", + "y" : 3 + }, + { + "drilldown" : "Athanasius", + "name" : "Athanasius", + "y" : 4 + }, + { + "drilldown" : "BarrOff", + "name" : "BarrOff", + "y" : 2 + }, + { + "drilldown" : "Bob Lied", + "name" : "Bob Lied", + "y" : 2 + }, + { + "drilldown" : "Bruce Gray", + "y" : 4, + "name" : "Bruce Gray" + }, + { + "name" : "Dave Jacoby", + "y" : 2, + "drilldown" : "Dave Jacoby" + }, + { + "drilldown" : "David Ferrone", + "name" : "David Ferrone", + "y" : 2 + }, + { + "drilldown" : "E. Choroba", + "y" : 2, + "name" : "E. Choroba" + }, + { + "drilldown" : "Feng Chang", + "y" : 2, + "name" : "Feng Chang" + }, + { + "y" : 5, + "name" : "Jaldhar H. Vyas", + "drilldown" : "Jaldhar H. Vyas" + }, + { + "y" : 2, + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek" + }, + { + "drilldown" : "Joelle Maslak", + "name" : "Joelle Maslak", + "y" : 4 + }, + { + "drilldown" : "Jorg Sommrey", + "name" : "Jorg Sommrey", + "y" : 3 + }, + { + "drilldown" : "Kai Burgdorf", + "y" : 2, + "name" : "Kai Burgdorf" + }, + { + "y" : 2, + "name" : "Kjetil Skotheim", + "drilldown" : "Kjetil Skotheim" + }, + { + "y" : 6, + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Luca Ferrari", + "y" : 11, + "name" : "Luca Ferrari" + }, + { + "name" : "Mariano Ortega", + "y" : 2, + "drilldown" : "Mariano Ortega" + }, + { + "drilldown" : "Mark Anderson", + "y" : 2, + "name" : "Mark Anderson" + }, + { + "drilldown" : "Matthew Neleigh", + "name" : "Matthew Neleigh", + "y" : 2 + }, + { + "drilldown" : "Matthias Muth", + "y" : 3, + "name" : "Matthias Muth" + }, + { + "y" : 2, + "name" : "Nelo Tovar", + "drilldown" : "Nelo Tovar" + }, + { + "y" : 2, + "name" : "Niels van Dijke", + "drilldown" : "Niels van Dijke" + }, + { + "drilldown" : "Packy Anderson", + "y" : 5, + "name" : "Packy Anderson" + }, + { + "drilldown" : "Peter Campbell Smith", + "y" : 3, + "name" : "Peter Campbell Smith" + }, + { + "name" : "Peter Meszaros", + "y" : 2, + "drilldown" : "Peter Meszaros" + }, + { + "y" : 3, + "name" : "Reinier Maliepaard", + "drilldown" : "Reinier Maliepaard" |
