diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-01-13 20:19:42 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-01-13 20:19:42 +0000 |
| commit | 70a1571ce4c48f53a1eebc84eddb5c035f88b987 (patch) | |
| tree | c921f735f6769026cd488bdddef7c769d9853e46 | |
| parent | c68c7807d88ae9b9f98377287374db24aca448e5 (diff) | |
| download | perlweeklychallenge-club-70a1571ce4c48f53a1eebc84eddb5c035f88b987.tar.gz perlweeklychallenge-club-70a1571ce4c48f53a1eebc84eddb5c035f88b987.tar.bz2 perlweeklychallenge-club-70a1571ce4c48f53a1eebc84eddb5c035f88b987.zip | |
- Added solutions by Robbie Hatley.
- Added solutions by Bob Lied.
- Added solutions by Arpad Thot.
- Added solutions by Jorg Sommrey.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Robert DiCicco.
24 files changed, 2322 insertions, 1904 deletions
diff --git a/challenge-199/laurent-rosenfeld/blog.txt b/challenge-199/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..31f6f623f2 --- /dev/null +++ b/challenge-199/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/01/perl-weekly-challenge-199-good-pairs-and-good-triplets.html diff --git a/challenge-199/laurent-rosenfeld/perl/ch-1.pl b/challenge-199/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..7672044f89 --- /dev/null +++ b/challenge-199/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use feature "say"; + +sub count_good_pairs { + my @in = @_; + my $count = 0; + for my $i (0..$#in-1) { + for my $j ($i+1..$#in) { + $count++ if $in[$i] == $in[$j]; + } + } + return $count; +} + +for my $test ( [1,2,3,1,1,3], [1,2,3], [1,1,1,1], + [1,2,3,1,2,3], [4,3,2,3,2,1] ) { + say sprintf "%-15s => %d", "@$test", count_good_pairs @$test; +} diff --git a/challenge-199/laurent-rosenfeld/perl/ch-2.pl b/challenge-199/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..c34ce0f239 --- /dev/null +++ b/challenge-199/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,25 @@ +sub count_good_triplets { + my @in = @{$_[0]}; + my ($x, $y, $z) = @{$_[1]}; + my $count = 0; + for my $i (0..$#in-2) { + for my $j ($i+1..$#in-1) { + # short-cut the $k loop if $i $j not good + next if abs($in[$i] - $in[$j]) > $x; + for my $k ($j+1..$#in) { + $count++ if abs($in[$j] - $in[$k]) <= $y + and abs($in[$i] - $in[$k]) <= $z; + } + } + } + return $count; +} + +for my $test ( [ [3,0,1,1,9,7], [7,2,3] ], + [ [1,1,2,2,3], [0,0,1] ], + [ [1,1,2,2,3], [1,1,2] ], + ) { + say sprintf "%-15s - xyz = %-10s => %d", + "@{@$test[0]}", "@{@$test[1]}", + count_good_triplets @$test; +} diff --git a/challenge-199/laurent-rosenfeld/raku/ch-1.raku b/challenge-199/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..15c475254a --- /dev/null +++ b/challenge-199/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,12 @@ +sub count_good_pairs (@in) { + my $cnt = 0; + for 0..^@in.end -> $i { + $cnt++ if @in[$i] == @in[$_] for $i+1..@in.end; + } + return $cnt; +} + +for <1 2 3 1 1 3>, <1 2 3>, <1 1 1 1>, + <1 2 3 1 2 3>, <4 3 2 3 2 1> -> @test { + say (~@test).fmt("%-15s => "), count_good_pairs @test; +} diff --git a/challenge-199/laurent-rosenfeld/raku/ch-2.raku b/challenge-199/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..e711920896 --- /dev/null +++ b/challenge-199/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,23 @@ +sub count_good_triplets (@in, @xyz) { + my $count = 0; + my ($x, $y, $z) = @xyz; + for 0..@in.end-2 -> $i { + for $i+1..^@in.end -> $j { + next if abs(@in[$i] - @in[$j]) > $x; + for $j+1..@in.end -> $k { + $count++ if abs(@in[$j] - @in[$k]) <= $y + && abs(@in[$i] - @in[$k]) <= $z; + } + } + } + return $count; +} + +for ( <3 0 1 1 9 7>, <7 2 3> ), + ( <1 1 2 2 3>, <0 0 1> ), + ( <1 1 2 2 3>, <1 1 2> ) -> @test { + + say sprintf "%-15s - xyz = %-10s => %d", + "@test[0]", "@test[1]", + count_good_triplets @test[0], @test[1]; +} diff --git a/challenge-199/robert-dicicco/perl/ch-2.pl b/challenge-199/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..d18144dfb1 --- /dev/null +++ b/challenge-199/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,119 @@ +#!/usr/bin/env perl + +=begin + +AUTHOR: Robert DiCicco + +DATE : 2023-01-12 + +Challenge 199 Good Triplets ( Perl ) + +=cut + +use strict; + +use warnings; + +use Algorithm::Permute; + +use List::MoreUtils qw/indexes/; + + + +my %seen = (); + + + +my @list = (3,0,1,1,9,7); + +my $x = 7; + +my $y = 2; + +my $z = 3; + + + +# my @list = (1,1,2,2,3); + +# my $x = 0; + +# my $y = 0; + +# my $z = 1; + + + + + +my $anchor = 0; + +my $cnt = 0; + + + +sub TestArray { + + my ($first, $second, $third) = @_; + + my @x1 = indexes { $_ == $first } (@list); + + my @x2 = indexes { $_ == $second } (@list); + + my @x3 = indexes { $_ == $third } (@list); + + if (($x1[0] > $x2[0]) || ($x2[0] > $x3[0]) || ($x1[0] > $x3[0]) ){ + + return -1; + + } + + if (abs($first - $second) > $x) { return -1}; + + if (abs($second - $third) > $y) { return -1}; + + if (abs($first - $third) > $z) { return -1}; + + return 1; + +} + +my $sz = @list; + +my $p = Algorithm::Permute->new(\@list,3); + +while (my @res = $p->next) { + + my $retval = TestArray(@res); + + my $combined = "$res[0]$res[1]$res[2]"; + + if ($retval == 1) { + + if (! $seen{$combined}){ + + $seen{$combined} = 1; + + } else { + + print "$res[0], $res[1], $res[2]\n"; + + } + + } + +} + +=begin + +SAMPLE OUTPUT + +perl .\GoodTriplets.pl + +0, 1, 1 + +3, 1, 1 + +3, 0, 1 + +=cut diff --git a/challenge-199/robert-dicicco/raku/ch-2.raku b/challenge-199/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..7b188e4e4f --- /dev/null +++ b/challenge-199/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,117 @@ +#!/usr/bin/env raku + +#`{ + +AUTHOR: Robert DiCicco + +DATE : 2023-01-12 + +Challenge 199 Good Triplets ( Raku ) + +} + +use v6; + + + +my %seen = (); + + + +my @list = (3,0,1,1,9,7); + +my $x = 7; + +my $y = 2; + +my $z = 3; + + + +# my @list = (1,1,2,2,3); + +# my $x = 0; + +# my $y = 0; + +# my $z = 1; + + + + + +my $cnt = 0; + + + +sub TestArray(@arr) { + + my $first=@arr[0]; + + my $second=@arr[1]; + + my $third=@arr[2]; + + my $teststr = join("",@list); + + my @x1 = $teststr.indices($first); + + my @x2 = $teststr.indices($second); + + my @x3 = $teststr.indices($third); + + if @x1[0] > @x2[0] || @x2[0] > @x3[0] || @x1[0] > @x3[0] { + + return -1; + + } + + if ($first - $second).abs > $x { return -1}; + + if ($second - $third).abs > $y { return -1}; + + if ($first - $third).abs > $z { return -1}; + + return 1; + +} + + + +for @list.combinations: 3 -> @res { + + my $retval = TestArray(@res); + + my $combined = "@res[0]@res[1]@res[2]"; + + if ($retval == 1) { + + if (! defined %seen{$combined}) { + + %seen{$combined} = 1; + + print "@res[0], @res[1], @res[2]\n"; + + } else { + + %seen{$combined}++; + + } + + } + +} + +#`{ + +SAMPLE OUTPUT + +raku .\GoodTriplets.rk + +3, 0, 1 + +3, 1, 1 + +0, 1, 1 + +} diff --git a/stats/pwc-challenge-189.json b/stats/pwc-challenge-189.json index ba174ac07a..76406280e4 100644 --- a/stats/pwc-challenge-189.json +++ b/stats/pwc-challenge-189.json @@ -1,49 +1,26 @@ { - "chart" : { - "type" : "column" - }, - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - } - } - }, - "tooltip" : { - "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/>", - "followPointer" : 1 - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, "series" : [ { - "name" : "The Weekly Challenge - 189", "data" : [ { - "y" : 4, "name" : "Adam Russell", - "drilldown" : "Adam Russell" + "drilldown" : "Adam Russell", + "y" : 4 }, { - "name" : "Alexander Pankoff", + "y" : 2, "drilldown" : "Alexander Pankoff", - "y" : 2 + "name" : "Alexander Pankoff" }, { - "name" : "Ali Moradi", "drilldown" : "Ali Moradi", + "name" : "Ali Moradi", "y" : 4 }, { - "name" : "Andrew Grangaard", + "y" : 2, "drilldown" : "Andrew Grangaard", - "y" : 2 + "name" : "Andrew Grangaard" }, { "name" : "Arne Sommer", @@ -51,9 +28,14 @@ "y" : 3 }, { - "y" : 4, "name" : "Athanasius", - "drilldown" : "Athanasius" + "drilldown" : "Athanasius", + "y" : 4 + }, + { + "drilldown" : "Bob Lied", + "name" : "Bob Lied", + "y" : 2 }, { "name" : "Bruce Gray", @@ -62,13 +44,13 @@ }, { "y" : 1, - "name" : "Cheok-Yin Fung", - "drilldown" : "Cheok-Yin Fung" + "drilldown" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung" }, { - "y" : 3, "drilldown" : "Colin Crain", - "name" : "Colin Crain" + "name" : "Colin Crain", + "y" : 3 }, { "name" : "Dave Jacoby", @@ -76,29 +58,29 @@ "y" : 2 }, { - "y" : 2, "drilldown" : "Duncan C. White", - "name" : "Duncan C. White" + "name" : "Duncan C. White", + "y" : 2 }, { "y" : 3, - "name" : "E. Choroba", - "drilldown" : "E. Choroba" + "drilldown" : "E. Choroba", + "name" : "E. Choroba" }, { - "y" : 2, + "name" : "Feng Chang", "drilldown" : "Feng Chang", - "name" : "Feng Chang" + "y" : 2 }, { - "y" : 6, "name" : "Flavio Poletti", - "drilldown" : "Flavio Poletti" + "drilldown" : "Flavio Poletti", + "y" : 6 }, { - "y" : 2, "name" : "Humberto Massa", - "drilldown" : "Humberto Massa" + "drilldown" : "Humberto Massa", + "y" : 2 }, { "y" : 5, @@ -106,24 +88,24 @@ "drilldown" : "Jaldhar H. Vyas" }, { - "y" : 3, "drilldown" : "James Smith", - "name" : "James Smith" + "name" : "James Smith", + "y" : 3 }, { + "y" : 1, "drilldown" : "Jan Krnavek", - "name" : "Jan Krnavek", - "y" : 1 + "name" : "Jan Krnavek" }, { - "name" : "Jorg Sommrey", "drilldown" : "Jorg Sommrey", + "name" : "Jorg Sommrey", "y" : 2 }, { - "y" : 2, "name" : "Kueppo Wesley", - "drilldown" : "Kueppo Wesley" + "drilldown" : "Kueppo Wesley", + "y" : 2 }, { "y" : 5, @@ -131,58 +113,58 @@ "drilldown" : "Laurent Rosenfeld" }, { - "y" : 4, + "drilldown" : "Luca Ferrari", "name" : "Luca Ferrari", - "drilldown" : "Luca Ferrari" + "y" : 4 }, { + "y" : 2, "name" : "Mark Anderson", - "drilldown" : "Mark Anderson", - "y" : 2 + "drilldown" : "Mark Anderson" }, { - "drilldown" : "Marton Polgar", "name" : "Marton Polgar", + "drilldown" : "Marton Polgar", "y" : 2 }, { - "name" : "Matthew Neleigh", "drilldown" : "Matthew Neleigh", + "name" : "Matthew Neleigh", "y" : 2 }, { - "y" : 3, "drilldown" : "Mohammad S Anwar", - "name" : "Mohammad S Anwar" + "name" : "Mohammad S Anwar", + "y" : 3 }, { - "y" : 2, + "name" : "Niels van Dijke", "drilldown" : "Niels van Dijke", - "name" : "Niels van Dijke" + "y" : 2 }, { - "drilldown" : "Peter Campbell Smith", + "y" : 3, "name" : "Peter Campbell Smith", - "y" : 3 + "drilldown" : "Peter Campbell Smith" }, { "y" : 2, - "drilldown" : "Robbie Hatley", - "name" : "Robbie Hatley" + "name" : "Robbie Hatley", + "drilldown" : "Robbie Hatley" }, { "y" : 2, - "drilldown" : "Robert DiCicco", - "name" : "Robert DiCicco" + "name" : "Robert DiCicco", + "drilldown" : "Robert DiCicco" }, { "y" : 2, - "drilldown" : "Robert Ransbottom", - "name" : "Robert Ransbottom" + "name" : "Robert Ransbottom", + "drilldown" : "Robert Ransbottom" }, { - "name" : "Roger Bell_West", "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West", "y" : 5 }, { @@ -191,9 +173,9 @@ "y" : 3 }, { - "y" : 2, "drilldown" : "Solathian", - "name" : "Solathian" + "name" : "Solathian", + "y" : 2 }, { "y" : 5, @@ -201,46 +183,49 @@ "name" : "Stephen G. Lynn" }, { - "y" : 2, "name" : "Tim Potapov", - "drilldown" : "Tim Potapov" + "drilldown" : "Tim Potapov", + "y" : 2 }, { - "name" : "Ulrich Rieke", "drilldown" : "Ulrich Rieke", + "name" : "Ulrich Rieke", "y" : 4 }, { + "y" : 2, "drilldown" : "Vamsi Meenavilli", - "name" : "Vamsi Meenavilli", - "y" : 2 + "name" : "Vamsi Meenavilli" }, { - "name" : "W. Luis Mochan", + "y" : 3, "drilldown" : "W. Luis Mochan", - "y" : 3 + "name" : "W. Luis Mochan" } ], + "name" : "The Weekly Challenge - 189", "colorByPoint" : 1 } ], "title" : { "text" : "The Weekly Challenge - 189" }, - "xAxis" : { - "type" : "category" + "chart" : { + "type" : "column" }, "legend" : { "enabled" : 0 }, - "subtitle" : { - "text" : "[Champions: 39] Last updated at 2022-11-07 13:35:44 GMT" + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } }, "drilldown" : { "series" : [ { - "name" : "Adam Russell", "id" : "Adam Russell", + "name" : "Adam Russell", "data" : [ [ "Perl", @@ -277,14 +262,14 @@ ] }, { + "name" : "Andrew Grangaard", + "id" : "Andrew Grangaard", "data" : [ [ "Perl", 2 ] - ], - "id" : "Andrew Grangaard", - "name" : "Andrew Grangaard" + ] }, { "name" : "Arne Sommer", @@ -315,6 +300,18 @@ "name" : "Athanasius" }, { + "id" : "Bob Lied", + "name" : "Bob Lied", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Bruce Gray", + "name" : "Bruce Gray", "data" : [ [ "Perl", @@ -328,19 +325,17 @@ "Blog", 1 ] - ], - "name" : "Bruce Gray", - "id" : "Bruce Gray" + ] }, { + "name" : "Cheok-Yin Fung", + "id" : "Cheok-Yin Fung", "data" : [ [ "Perl", 1 ] - ], - "name" : "Cheok-Yin Fung", - "id" : "Cheok-Yin Fung" + ] }, { "data" : [ @@ -357,18 +352,18 @@ "name" : "Colin Crain" }, { - "id" : "Dave Jacoby", - "name" : "Dave Jacoby", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Dave Jacoby", + "id" : "Dave Jacoby" }, { - "id" : "Duncan C. White", "name" : "Duncan C. White", + "id" : "Duncan C. White", "data" : [ [ "Perl", @@ -377,6 +372,8 @@ ] }, { + "id" : "E. Choroba", + "name" : "E. Choroba", "data" : [ [ "Perl", @@ -386,19 +383,17 @@ "Blog", 1 ] - ], - "name" : "E. Choroba", - "id" : "E. Choroba" + ] }, { - "id" : "Feng Chang", - "name" : "Feng Chang", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Feng Chang", + "id" : "Feng Chang" }, { "data" : [ @@ -415,22 +410,20 @@ 2 ] ], - "id" : "Flavio Poletti", - "name" : "Flavio Poletti" + "name" : "Flavio Poletti", + "id" : "Flavio Poletti" }, { - "name" : "Humberto Massa", - "id" : "Humberto Massa", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Humberto Massa", + "id" : "Humberto Massa" }, { - "id" : "Jaldhar H. Vyas", - "name" : "Jaldhar H. Vyas", "data" : [ [ "Perl", @@ -444,11 +437,13 @@ "Blog", 1 ] - ] + ], + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" }, { - "name" : "James Smith", "id" : "James Smith", + "name" : "James Smith", "data" : [ [ "Perl", @@ -461,38 +456,36 @@ ] }, { + "name" : "Jan Krnavek", + "id" : "Jan Krnavek", "data" : [ [ "Raku", 1 ] - ], - "id" : "Jan Krnavek", - "name" : "Jan Krnavek" + ] }, { - "name" : "Jorg Sommrey", - "id" : "Jorg Sommrey", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey" }, { - "id" : "Kueppo Wesley", - "name" : "Kueppo Wesley", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Kueppo Wesley", + "name" : "Kueppo Wesley" }, { - "name" : "Laurent Rosenfeld", - "id" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -506,7 +499,9 @@ "Blog", 1 ] - ] + ], + "name" : "Laurent Rosenfeld", + "id" : "Laurent Rosenfeld" }, { "data" : [ @@ -523,38 +518,36 @@ "name" : "Luca Ferrari" }, { - "name" : "Mark Anderson", - "id" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Mark Anderson", + "id" : "Mark Anderson" }, { + "name" : "Marton Polgar", + "id" : "Marton Polgar", "data" : [ [ "Raku", 2 ] - ], - "id" : "Marton Polgar", - "name" : "Marton Polgar" + ] }, { - "id" : "Matthew Neleigh", - "name" : "Matthew Neleigh", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Matthew Neleigh", + "id" : "Matthew Neleigh" }, { - "id" : "Mohammad S Anwar", - "name" : "Mohammad S Anwar", "data" : [ [ "Perl", @@ -564,21 +557,21 @@ "Raku", 1 ] - ] + ], + "id" : "Mohammad S Anwar", + "name" : "Mohammad S Anwar" }, { - "name" : "Niels van Dijke", - "id" : "Niels van Dijke", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Niels van Dijke", + "id" : "Niels van Dijke" }, { - "name" : "Peter Campbell Smith", - "id" : "Peter Campbell Smith", "data" : [ [ "Perl", @@ -588,17 +581,19 @@ "Blog", 1 ] - ] + ], + "name" : "Peter Campbell Smith", + "id" : "Peter Campbell Smith" }, { + "name" : "Robbie Hatley", + "id" : "Robbie Hatley", "data" : [ [ "Perl", 2 ] - ], - "id" : "Robbie Hatley", - "name" : "Robbie Hatley" + ] }, { "name" : "Robert DiCicco", @@ -615,16 +610,18 @@ ] }, { - "id" : "Robert Ransbottom", - "name" : "Robert Ransbottom", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Robert Ransbottom", + "name" : "Robert Ransbottom" |
