diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-01-16 05:58:14 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-01-16 05:58:14 +0000 |
| commit | e1e9c7e5b58d477779201ff17b22343dc17fcf0c (patch) | |
| tree | 787465932761be35557aa2648c891a09558d8954 /challenge-199 | |
| parent | 2ae8ab14ede2fcf9bd82127f114c27006e59b744 (diff) | |
| download | perlweeklychallenge-club-e1e9c7e5b58d477779201ff17b22343dc17fcf0c.tar.gz perlweeklychallenge-club-e1e9c7e5b58d477779201ff17b22343dc17fcf0c.tar.bz2 perlweeklychallenge-club-e1e9c7e5b58d477779201ff17b22343dc17fcf0c.zip | |
- Added solutions by Flavio Poletti.
Diffstat (limited to 'challenge-199')
| -rw-r--r-- | challenge-199/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-199/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-199/polettix/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-199/polettix/perl/ch-2.pl | 23 | ||||
| -rw-r--r-- | challenge-199/polettix/raku/ch-1.raku | 4 | ||||
| -rw-r--r-- | challenge-199/polettix/raku/ch-2.raku | 16 |
6 files changed, 49 insertions, 0 deletions
diff --git a/challenge-199/polettix/blog.txt b/challenge-199/polettix/blog.txt new file mode 100644 index 0000000000..f8803b551b --- /dev/null +++ b/challenge-199/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2023/01/12/pwc199-good-pairs/ diff --git a/challenge-199/polettix/blog1.txt b/challenge-199/polettix/blog1.txt new file mode 100644 index 0000000000..e0de2d9ee6 --- /dev/null +++ b/challenge-199/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2023/01/13/pwc199-good-triplets/ diff --git a/challenge-199/polettix/perl/ch-1.pl b/challenge-199/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..23bdca6de9 --- /dev/null +++ b/challenge-199/polettix/perl/ch-1.pl @@ -0,0 +1,4 @@ +#!/usr/bin/env perl +use v5.24; +say good_pairs(@ARGV ? @ARGV : (1, 2, 3, 1, 1, 3)); +sub good_pairs { my $s = 0; my %c; $s += $c{$_}++ for @_; $s } diff --git a/challenge-199/polettix/perl/ch-2.pl b/challenge-199/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..30e094dc38 --- /dev/null +++ b/challenge-199/polettix/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +my ($x, $y, $z, @list) = @ARGV ? @ARGV : (7, 2, 3, 3, 0, 1, 1, 9, 7); +say good_triplets($x, $y, $z, @list); + +sub good_triplets ($x, $y, $z, @list) { + my $count = 0; + for my $i (0 .. $#list - 2) { + for my $j ($i + 1 .. $#list - 1) { + next if abs($list[$i] - $list[$j]) > $x; + for my $k ($j + 1 .. $#list) { + next if abs($list[$j] - $list[$k]) > $y + || abs($list[$i] - $list[$k]) > $z; + ++$count; + } + } + } + return $count; +} diff --git a/challenge-199/polettix/raku/ch-1.raku b/challenge-199/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..28c51ee959 --- /dev/null +++ b/challenge-199/polettix/raku/ch-1.raku @@ -0,0 +1,4 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (*@args) { put good-pairs(@args ?? @args !! (1, 2, 3, 1, 1, 3))} +sub good-pairs (*@list) { my %c; @list.map({%c{$_}++}).sum } diff --git a/challenge-199/polettix/raku/ch-2.raku b/challenge-199/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..9c3c7a8691 --- /dev/null +++ b/challenge-199/polettix/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku +use v6; +sub MAIN ($x, $y, $z, *@list) { put good-triplets($x, $y, $z, @list) } + +sub good-triplets ($x, $y, $z, *@list) { + return [+] gather for 0 .. (@list - 3) -> \i { + for (i + 1) .. (@list - 2) -> \j { + next if (@list[i] - @list[j]).abs > $x; + for (j + 1) ..^ @list -> \k { + next if (@list[j] - @list[k]).abs > $y + || (@list[i] - @list[k]).abs > $z; + take 1; + } + } + }; +} |
