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 /challenge-199 | |
| 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.
Diffstat (limited to 'challenge-199')
| -rw-r--r-- | challenge-199/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-199/laurent-rosenfeld/perl/ch-1.pl | 19 | ||||
| -rw-r--r-- | challenge-199/laurent-rosenfeld/perl/ch-2.pl | 25 | ||||
| -rw-r--r-- | challenge-199/laurent-rosenfeld/raku/ch-1.raku | 12 | ||||
| -rw-r--r-- | challenge-199/laurent-rosenfeld/raku/ch-2.raku | 23 | ||||
| -rw-r--r-- | challenge-199/robert-dicicco/perl/ch-2.pl | 119 | ||||
| -rw-r--r-- | challenge-199/robert-dicicco/raku/ch-2.raku | 117 |
7 files changed, 316 insertions, 0 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 + +} |
