From cc257dea6bf637681e4b322398232c6f566462a6 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Tue, 3 Jan 2023 15:12:43 +0800 Subject: challenge 198, raku solutions --- challenge-198/feng-chang/raku/ch-1.raku | 8 ++++++++ challenge-198/feng-chang/raku/ch-1a.raku | 12 ++++++++++++ challenge-198/feng-chang/raku/ch-2.raku | 5 +++++ 3 files changed, 25 insertions(+) create mode 100755 challenge-198/feng-chang/raku/ch-1.raku create mode 100755 challenge-198/feng-chang/raku/ch-1a.raku create mode 100755 challenge-198/feng-chang/raku/ch-2.raku diff --git a/challenge-198/feng-chang/raku/ch-1.raku b/challenge-198/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..2f214b0327 --- /dev/null +++ b/challenge-198/feng-chang/raku/ch-1.raku @@ -0,0 +1,8 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +if +@N < 2 { 0.put; exit } + +my \max-gap = (^(@N.elems-1)).map({ @N[$_+1] - @N[$_] }).max; +put (^(@N.elems-1)).grep({ max-gap == @N[$_+1] - @N[$_] }).elems; diff --git a/challenge-198/feng-chang/raku/ch-1a.raku b/challenge-198/feng-chang/raku/ch-1a.raku new file mode 100755 index 0000000000..2603b422ba --- /dev/null +++ b/challenge-198/feng-chang/raku/ch-1a.raku @@ -0,0 +1,12 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +if +@N < 2 { 0.put; exit } + +^ (@N.elems - 1) ==> +map { @N[$_+1] - @N[$_] } ==> +my @gaps; @gaps ==> +grep { $_ == @gaps.max } ==> +elems() ==> +put(); diff --git a/challenge-198/feng-chang/raku/ch-2.raku b/challenge-198/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..61da78dcfd --- /dev/null +++ b/challenge-198/feng-chang/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(UInt:D \m); + +put (^m).grep(*.is-prime).elems; -- cgit From 80550f6138b2787f9d77365577e5ae1df593d9ff Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 9 Jan 2023 08:27:03 +0100 Subject: Task 1 done --- challenge-199/luca-ferrari/raku/ch-1.p6 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 challenge-199/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-199/luca-ferrari/raku/ch-1.p6 b/challenge-199/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..3387fc252b --- /dev/null +++ b/challenge-199/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,16 @@ +#!raku + +# Perl Weekly Challenge 199 + +sub MAIN( Bool :$verbose = False, + *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + my @pair-indexes; + for 0 ..^ @list.elems - 1 -> $i { + for $i + 1 ..^ @list.elems -> $j { + @pair-indexes.push: [ $i, $j, @list[ $i ], @list[ $j ] ] if @list[ $i ] == @list[ $j ]; + } + } + + @pair-indexes.elems.say; + @pair-indexes.map( { 'Idexes ' ~ $_[0] ~ ',' ~ $_[1] ~ ' refer to equal elements ' ~ $_[2] ~ ',' ~ $_[3] } ).join( "\n" ).say if $verbose; +} -- cgit From aa53c8f739c6435b7d545b68ccd72a73e1e5d331 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 9 Jan 2023 09:04:33 +0100 Subject: Task 2 done --- challenge-199/luca-ferrari/raku/ch-2.p6 | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-199/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-199/luca-ferrari/raku/ch-2.p6 b/challenge-199/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..05cc0159a8 --- /dev/null +++ b/challenge-199/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,33 @@ +#!raku + +# Perl Weekly Challenge 199 + +sub MAIN( + Int $x, + Int $y, + Int $z, + Bool :$verbose = False, + *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + + +# a) 0 <= i < j < k <= n (size of given array) +# b) abs(array[i] - array[j]) <= x +# c) abs(array[j] - array[k]) <= y +# d) abs(array[i] - array[k]) <= z + + my @triplets; + for 0 ..^ @list.elems -> $i { + for $i ^..^@list.elems -> $j { + for $j ^..^ @list.elems -> $k { + @triplets.push: [ $i, $j, $k, @list[ $i ], @list[ $j ], @list[ $k ] ] + if ( ( @list[ $i ] - @list[ $j ] ).abs <= $x + && ( @list[ $j ] - @list[ $k ] ).abs <= $y + && ( @list[ $i ] - @list[ $k ] ).abs <= $z ); + + } + } + } + + @triplets.elems.say; + @triplets.map( { "Indexes $_[0], $_[1], $_[2] are good ($_[3], $_[4], $_[5])" } ).join( "\n" ).say; +} -- cgit From c1382daf85cc9f5a7d2061ffd4c32631055ace61 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 9 Jan 2023 08:46:47 +0000 Subject: Challenge 199 Solutions (Raku) --- challenge-199/mark-anderson/raku/ch-1.raku | 13 +++++++++++++ challenge-199/mark-anderson/raku/ch-2.raku | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 challenge-199/mark-anderson/raku/ch-1.raku create mode 100644 challenge-199/mark-anderson/raku/ch-2.raku diff --git a/challenge-199/mark-anderson/raku/ch-1.raku b/challenge-199/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..75aaa9178a --- /dev/null +++ b/challenge-199/mark-anderson/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use Test; + +is good-pairs(1,2,3,1,1,3), 4; +is good-pairs(1,2,3), 0; +is good-pairs(1,1,1,1), 6; + +sub good-pairs(+$list) +{ + $list.combinations(2) + .grep({ .[0] == .[1] }) + .elems +} diff --git a/challenge-199/mark-anderson/raku/ch-2.raku b/challenge-199/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..c9870d4fe9 --- /dev/null +++ b/challenge-199/mark-anderson/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku +use Test; + +is good-triplets((3,0,1,1,9,7), 7, 2, 3), 4; +is good-triplets((1,1,2,2,3), 0, 0, 1), 0; + +sub good-triplets($list, $x, $y, $z) +{ + $list.combinations(3) + .grep({ + [and] abs(.[0] - .[1]) <= $x, + abs(.[1] - .[2]) <= $y, + abs(.[0] - .[2]) <= $z + }) + .elems +} -- cgit From dec095313737f0489797225861addabcf8e5a4f0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 9 Jan 2023 10:02:23 +0100 Subject: Task 1 plperl --- challenge-199/luca-ferrari/postgresql/ch-1.plperl | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 challenge-199/luca-ferrari/postgresql/ch-1.plperl diff --git a/challenge-199/luca-ferrari/postgresql/ch-1.plperl b/challenge-199/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..7b91f9a3f9 --- /dev/null +++ b/challenge-199/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,24 @@ +-- Perl Weekly Challenge 199 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc199; + +CREATE OR REPLACE FUNCTION +pwc199.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + + my ( $list ) = @_; + my @pairs; + + for my $i ( 0 .. $list->@* ) { + for my $j ( $i .. $list->@* ) { + next if $i == $j; + push @pairs, [ $i, $j ] if ( $list->[ $i ] == $list->[ $j ] ); + } + } + + return scalar @pairs; + +$CODE$ +LANGUAGE plperl; -- cgit From 41df53aa3f75854aa565cbe4911ac16edaec3668 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 9 Jan 2023 10:17:21 +0100 Subject: Task 2 plperl done --- challenge-199/luca-ferrari/postgresql/ch-2.plperl | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 challenge-199/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-199/luca-ferrari/postgresql/ch-2.plperl b/challenge-199/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..5063643e27 --- /dev/null +++ b/challenge-199/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,34 @@ +-- Perl Weekly Challenge 199 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc199; + +/* +estdb=> select pwc199.task2_plperl( 7,2,3, array[3,0,1,1,9,7]::int[] ); + task2_plperl +-------------- + 4 + +*/ +CREATE OR REPLACE FUNCTION +pwc199.task2_plperl( int, int, int, int[] ) +RETURNS int +AS $CODE$ + my ( $x, $y, $z, $list ) = @_; + my @triplets; + + for my $i ( 0 .. $list->@* ) { + for my $j ( $i + 1 .. $list->@* - 1 ) { + for my $k ( $j + 1 .. $list->@* - 2 ) { + + push @triplets, [ $i, $j, $k ] if ( abs( $list->[ $i ] - $list->[ $j ] ) <= $x + && abs( $list->[ $j ] - $list->[ $k ] ) <= $y + && abs( $list->[ $i ] - $list->[ $k ] ) <= $z ); + } + } + } + + + return scalar @triplets; +$CODE$ +LANGUAGE plperl; -- cgit From ff0fa43a9068b7666da137a2337b23a82d6133b8 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 9 Jan 2023 10:20:06 +0100 Subject: Task 1 plpgsql done --- challenge-199/luca-ferrari/postgresql/ch-1.sql | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-199/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-199/luca-ferrari/postgresql/ch-1.sql b/challenge-199/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..0eda98d91a --- /dev/null +++ b/challenge-199/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,30 @@ +-- Perl Weekly Challenge 199 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc199; + +CREATE OR REPLACE FUNCTION +pwc199.task1_plpgsql( l int[] ) +RETURNS int +AS $CODE$ +DECLARE + i int; + j int; + c int := 0; +BEGIN + FOR i IN 1 .. array_length( l, 1 ) LOOP + FOR j IN i .. array_length( l , 1 ) LOOP + If i = j THEN + CONTINUE; + END IF; + + IF l[i] = l[j] THEN + c := c + 1; + END IF; + END LOOP; + END LOOP; + + RETURN c; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 44d776efa78245641c7b2a9ad0826f83d43604a8 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 9 Jan 2023 10:22:57 +0100 Subject: Task 2 plpgsql done --- challenge-199/luca-ferrari/postgresql/ch-2.sql | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-199/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-199/luca-ferrari/postgresql/ch-2.sql b/challenge-199/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..0fcdaff71a --- /dev/null +++ b/challenge-199/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,29 @@ +-- Perl Weekly Challenge 199 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc199; + +CREATE OR REPLACE FUNCTION +pwc199.task2_plpgsql( x int, y int, z int, l int[] ) +RETURNS int +AS $CODE$ +DECLARE + i int; + j int; + k int; + c int := 0; +BEGIN + FOR i IN 1 .. array_length( l, 1 ) LOOP + FOR j IN ( i + 1 ) .. array_length( l, 1 ) LOOP + FOR k IN ( j + 1 ) .. array_length( l, 1 ) LOOP + IF abs( l[i] - l[j] ) <= x AND abs( l[j] - l[k] ) <= y AND abs( l[i] - l[k] ) <= z THEN + c := c + 1; + END IF; + END LOOP; + END LOOP; + END LOOP; + + RETURN c; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 7bf229e782fe3a61ff57bbbc39dab2bb3eb005c8 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 9 Jan 2023 17:26:02 +0800 Subject: challenge 199, raku solutions --- challenge-199/feng-chang/raku/ch-1.raku | 7 +++++++ challenge-199/feng-chang/raku/ch-1a.raku | 5 +++++ challenge-199/feng-chang/raku/ch-2.raku | 9 +++++++++ 3 files changed, 21 insertions(+) create mode 100755 challenge-199/feng-chang/raku/ch-1.raku create mode 100755 challenge-199/feng-chang/raku/ch-1a.raku create mode 100755 challenge-199/feng-chang/raku/ch-2.raku diff --git a/challenge-199/feng-chang/raku/ch-1.raku b/challenge-199/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..5b485fd2cb --- /dev/null +++ b/challenge-199/feng-chang/raku/ch-1.raku @@ -0,0 +1,7 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +put @N.unique.map(-> \j { + @N.grep(j, :k).combinations(2).elems +}).sum; diff --git a/challenge-199/feng-chang/raku/ch-1a.raku b/challenge-199/feng-chang/raku/ch-1a.raku new file mode 100755 index 0000000000..c195fde90f --- /dev/null +++ b/challenge-199/feng-chang/raku/ch-1a.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +put @N.Bag.values.map({ $_*($_-1)/2 }).sum; diff --git a/challenge-199/feng-chang/raku/ch-2.raku b/challenge-199/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..486651d5ec --- /dev/null +++ b/challenge-199/feng-chang/raku/ch-2.raku @@ -0,0 +1,9 @@ +#!/bin/env raku + +unit sub MAIN(UInt:D $x, UInt:D $y, UInt:D $z, *@N); + +put (^+@N).combinations(3).grep({ + abs(@N[.[0]] - @N[.[1]]) ≤ $x && + abs(@N[.[1]] - @N[.[2]]) ≤ $y && + abs(@N[.[2]] - @N[.[0]]) ≤ $z +}).elems; -- cgit From e75035e4a753f914fa5ecf79ac6fa6ba11cf737f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 9 Jan 2023 10:43:23 +0100 Subject: Blog references --- challenge-199/luca-ferrari/blog-1.txt | 1 + challenge-199/luca-ferrari/blog-2.txt | 1 + challenge-199/luca-ferrari/blog-3.txt | 1 + challenge-199/luca-ferrari/blog-4.txt | 1 + challenge-199/luca-ferrari/blog-5.txt | 1 + challenge-199/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-199/luca-ferrari/blog-1.txt create mode 100644 challenge-199/luca-ferrari/blog-2.txt create mode 100644 challenge-199/luca-ferrari/blog-3.txt create mode 100644 challenge-199/luca-ferrari/blog-4.txt create mode 100644 challenge-199/luca-ferrari/blog-5.txt create mode 100644 challenge-199/luca-ferrari/blog-6.txt diff --git a/challenge-199/luca-ferrari/blog-1.txt b/challenge-199/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..8c4c66cc1d --- /dev/null +++ b/challenge-199/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/09/PerlWeeklyChallenge199.html#task1 diff --git a/challenge-199/luca-ferrari/blog-2.txt b/challenge-199/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e68ed0b365 --- /dev/null +++ b/challenge-199/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/09/PerlWeeklyChallenge199.html#task2 diff --git a/challenge-199/luca-ferrari/blog-3.txt b/challenge-199/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..512ab8604a --- /dev/null +++ b/challenge-199/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/09/PerlWeeklyChallenge199.html#task1plperl diff --git a/challenge-199/luca-ferrari/blog-4.txt b/challenge-199/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..8691d12193 --- /dev/null +++ b/challenge-199/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/09/PerlWeeklyChallenge199.html#task2plperl diff --git a/challenge-199/luca-ferrari/blog-5.txt b/challenge-199/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..f4863bd5a1 --- /dev/null +++ b/challenge-199/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/09/PerlWeeklyChallenge199.html#task1plpgsql diff --git a/challenge-199/luca-ferrari/blog-6.txt b/challenge-199/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..56ea3afbd6 --- /dev/null +++ b/challenge-199/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/09/PerlWeeklyChallenge199.html#task2plpgsql -- cgit From 38d7d1321e2692eab2240c651f0c49104e5381eb Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 9 Jan 2023 10:29:07 +0000 Subject: w199 - Task 1 & 2 --- challenge-199/perlboy1967/perl/ch-1.pl | 44 ++++++++++++++++++++++++++ challenge-199/perlboy1967/perl/ch-2.pl | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 challenge-199/perlboy1967/perl/ch-1.pl create mode 100755 challenge-199/perlboy1967/perl/ch-2.pl diff --git a/challenge-199/perlboy1967/perl/ch-1.pl b/challenge-199/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..a1389d07e4 --- /dev/null +++ b/challenge-199/perlboy1967/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 199 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-199/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Good Pairs +Submitted by: Mohammad S Anwar + +You are given a list of integers, @list. + +Write a script to find the total count of Good Pairs. + +|| A pair (i, j) is called good if list[i] == list[j] and i < j. + +=cut + +use v5.16; +use common::sense; + + +use Test::More; +use Test::Deep qw(cmp_deeply); + + +sub goodPairs { + my $ar = []; + for my $i (0 .. scalar(@_) - 1) { + for my $j ($i + 1 .. scalar(@_) - 1) { + push(@$ar,[$i,$j]) if ($_[$i] == $_[$j] and $i < $j); + } + } + return $ar; +} + + +cmp_deeply(goodPairs(1,2,3,1,1,3),[[0,3],[0,4],[2,5],[3,4]]); +cmp_deeply(goodPairs(1,2,3),[]); +cmp_deeply(goodPairs(1,1,1,1),[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]); + +done_testing; diff --git a/challenge-199/perlboy1967/perl/ch-2.pl b/challenge-199/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..ab5f977a81 --- /dev/null +++ b/challenge-199/perlboy1967/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 199 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-199/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Good Triplets +Submitted by: Mohammad S Anwar + +You are given an array of integers, @array and three integers $x,$y,$z. + +Write a script to find out total Good Triplets in the given array. + +A triplet array[i], array[j], array[k] is good if it satisfies the following conditions: + +a) 0 <= i < j < k <= n (size of given array) +b) abs(array[i] - array[j]) <= x +c) abs(array[j] - array[k]) <= y +d) abs(array[i] - array[k]) <= z + +=cut + +use v5.16; +use common::sense; + + +use Test::More; +use Test::Deep qw(cmp_deeply); + + +sub goodTriplets ($$$\@) { + my ($x,$y,$z,$arIn) = @_; + + my $arOut = []; + for my $i (0 .. scalar(@_) - 1) { + for my $j ($i + 1 .. scalar(@_) - 1) { + for my $k ($j + 1 .. scalar(@_) - 1) { + push(@$arOut,[$$arI[$i],$$arI[$j],$$arI[$k]]) + if (abs($$arI[$i] - $$arI[$j]) <= $x and + abs($$arI[$j] - $$arI[$k]) <= $y and + abs($$arI[$i] - $$arI[$k]) <= $z); + } + } + } + + return $arOut; +} + + +cmp_deeply(goodTriplets(7,2,3,@{[3,0,1,1,9,7]}), + [[3,0,1],[3,0,1],[3,1,1],[0,1,1]]); +cmp_deeply(goodTriplets(0,0,1,@{[1,1,2,2,3]}), + []); + +done_testing; -- cgit From eab50459cf1a24866212bea895655b2d1c63f9cb Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Mon, 9 Jan 2023 12:44:56 +0100 Subject: challenge 199 by spadacciniweb --- challenge-199/spadacciniweb/README | 1 + challenge-199/spadacciniweb/perl/ch-1.pl | 14 +++++++++++++ challenge-199/spadacciniweb/perl/ch-2.pl | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 challenge-199/spadacciniweb/README create mode 100644 challenge-199/spadacciniweb/perl/ch-1.pl create mode 100644 challenge-199/spadacciniweb/perl/ch-2.pl diff --git a/challenge-199/spadacciniweb/README b/challenge-199/spadacciniweb/README new file mode 100644 index 0000000000..8d335de4c1 --- /dev/null +++ b/challenge-199/spadacciniweb/README @@ -0,0 +1 @@ +Solution by Mariano Spadaccini. diff --git a/challenge-199/spadacciniweb/perl/ch-1.pl b/challenge-199/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..e6840eea49 --- /dev/null +++ b/challenge-199/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +my @list = @ARGV; +die 'list elements < 2' + if scalar @list < 2; + +my $output = 0; +foreach my $i (0..$#list-1) { + $output += scalar map { $list[$i] == $list[$_] ? 1 : () } + ($i+1..$#list); +} +printf 'Output: %s', $output; diff --git a/challenge-199/spadacciniweb/perl/ch-2.pl b/challenge-199/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..cd5cf1fc00 --- /dev/null +++ b/challenge-199/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use strict; +use warnings; + + +print "List elements: "; +my @list = split /\s+/, ; +print "Three integers: "; +my @integers = split /\s+/, ; + +die 'input not valid' + if scalar @list < 3 + or + scalar @integers < 3 + or + (scalar map { $_ =~ /^[+\-]?\d+$/ ? 1 : () } @list) != (scalar @list) + or + scalar map { $_ =~ /^[+\-]?\d+$/ } @integers != 3 +; + +my ($x, $y, $z) = @integers; +my $output = 0; +foreach my $i (0..$#list-2) { + foreach my $j ($i+1..$#list-1) { + $output += scalar map { 1 } + grep { + abs($list[$i] - $list[$j]) <= $x + and + abs($list[$j] - $list[$_]) <= $y + and + abs($list[$i] - $list[$_]) <= $z + } ($j+1..$#list); + } +} +printf 'Output: %s', $output; -- cgit From 5a0788813aef7758a1aa7ff8e5146f031f6d2647 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 9 Jan 2023 14:59:21 +0000 Subject: Fixed typo --- challenge-199/perlboy1967/perl/ch-2.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-199/perlboy1967/perl/ch-2.pl b/challenge-199/perlboy1967/perl/ch-2.pl index ab5f977a81..523c272b95 100755 --- a/challenge-199/perlboy1967/perl/ch-2.pl +++ b/challenge-199/perlboy1967/perl/ch-2.pl @@ -38,10 +38,10 @@ sub goodTriplets ($$$\@) { for my $i (0 .. scalar(@_) - 1) { for my $j ($i + 1 .. scalar(@_) - 1) { for my $k ($j + 1 .. scalar(@_) - 1) { - push(@$arOut,[$$arI[$i],$$arI[$j],$$arI[$k]]) - if (abs($$arI[$i] - $$arI[$j]) <= $x and - abs($$arI[$j] - $$arI[$k]) <= $y and - abs($$arI[$i] - $$arI[$k]) <= $z); + push(@$arOut,[$$arIn[$i],$$arIn[$j],$$arIn[$k]]) + if (abs($$arIn[$i] - $$arIn[$j]) <= $x and + abs($$arIn[$j] - $$arIn[$k]) <= $y and + abs($$arIn[$i] - $$arIn[$k]) <= $z); } } } -- cgit From d5453eb676476ef5d280af810b5e77126ed196bd Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 9 Jan 2023 10:35:19 -0500 Subject: DAJ 199 --- challenge-199/dave-jacoby/perl/ch-1.pl | 34 +++++++++++++++++++++++++ challenge-199/dave-jacoby/perl/ch-2.pl | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 challenge-199/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-199/dave-jacoby/perl/ch-2.pl diff --git a/challenge-199/dave-jacoby/perl/ch-1.pl b/challenge-199/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..1685992dbd --- /dev/null +++ b/challenge-199/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( + + [ 1, 2, 3, 1, 1, 3 ], + [ 1, 2, 3 ], + [ 1, 1, 1, 1 ], + +); + +for my $e (@examples) { + my @list = $e->@*; + my $out = good_pairs(@list); + my $list = join ',', @list; + say <<"END"; + Input: \@list = ($list) + Output: $out +END +} + +sub good_pairs ( @list ) { + my $out = 0; + my $max = -1 + scalar @list; + for my $i ( 0 .. $max ) { + for my $j ( $i + 1 .. $max ) { + $out++ if $list[$i] == $list[$j]; + } + } + return $out; +} diff --git a/challenge-199/dave-jacoby/perl/ch-2.pl b/challenge-199/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..5275629dec --- /dev/null +++ b/challenge-199/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; +use Algorithm::Permute; + +my @examples = ( + + [ 7, 2, 3, 3, 0, 1, 1, 9, 7 ], + [ 0, 0, 1, 1, 1, 2, 2, 3 ], + +); + +for my $e (@examples) { + my $out = good_triplets( $e->@* ); + my ( $x, $y, $z, @array ) = $e->@*; + my $list = join ',', @array; + say <<"END"; + Input: \@array = ($list) and \$x = $x, \$y = $y, \$z = $z + Output: $out +END +} + +sub good_triplets ( $x, $y, $z, @array ) { + my $out = 0; + my $max = -1 + scalar @array; + for my $i ( 0 .. $max ) { + for my $j ( $i + 1 .. $max ) { + for my $k ( $j + 1 .. $max ) { + my $ij = abs( $array[$i] - $array[$j] ); + my $jk = abs( $array[$j] - $array[$k] ); + my $ik = abs( $array[$i] - $array[$k] ); + next unless $ij <= $x; + next unless $jk <= $y; + next unless $ik <= $z; + $out ++; + } + } + } + + say join ' ', $x, $y, $z, '|', @array; + return $out; +} + -- cgit From 0ae92695ac2d905eb58020ac15f2f343feabc4ae Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 9 Jan 2023 10:55:22 -0500 Subject: Week 199 --- challenge-199/zapwai/perl/ch-1.pl | 17 +++++++++++++++++ challenge-199/zapwai/perl/ch-2.pl | 22 ++++++++++++++++++++++ challenge-199/zapwai/raku/ch-1.raku | 15 +++++++++++++++ challenge-199/zapwai/raku/ch-2.raku | 15 +++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 challenge-199/zapwai/perl/ch-1.pl create mode 100644 challenge-199/zapwai/perl/ch-2.pl create mode 100644 challenge-199/zapwai/raku/ch-1.raku create mode 100644 challenge-199/zapwai/raku/ch-2.raku diff --git a/challenge-199/zapwai/perl/ch-1.pl b/challenge-199/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..9df9550e44 --- /dev/null +++ b/challenge-199/zapwai/perl/ch-1.pl @@ -0,0 +1,17 @@ +use v5.30.0; +my @list = (1,2,3,1,1,3); +#my @list =(1,2,3); +my $cnt = 0; +my $str; +for my $this (0 .. $#list - 1) { + for my $that ($this + 1 .. $#list) { + if ($list[$this] == $list[$that]) { + $str .= "($this, $that)\n"; + $cnt++; + } + } +} +say "Input: (". join(",",@list).")"; +say "Output: $cnt"; +chomp $str; +say $str; diff --git a/challenge-199/zapwai/perl/ch-2.pl b/challenge-199/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..0e51f64601 --- /dev/null +++ b/challenge-199/zapwai/perl/ch-2.pl @@ -0,0 +1,22 @@ +use v5.30.0; +my @list = (3,0,1,1,9,7); +my $x = 7; +my $y = 2; +my $z = 3; +say "Input: (".join(",",@list).")"; +print "Output: "; +my $cnt = 0; +my $str; +for my $i ( 0 .. $#list - 2) { + for my $j ( $i + 1 .. $#list - 1) { + for my $k ( $j + 1 .. $#list) { + if ( (abs($list[$i] - $list[$j]) <= $x) && (abs($list[$j] - $list[$k]) <= $y) && (abs($list[$i] - $list[$k]) <= $z) ) { + $str .= "($list[$i],$list[$j],$list[$k]) \t [(i,j,k) = ($i,$j,$k)]\n"; + $cnt++; + } + } + } +} + +chomp $str; +say "$cnt friendly triplets...\n$str"; diff --git a/challenge-199/zapwai/raku/ch-1.raku b/challenge-199/zapwai/raku/ch-1.raku new file mode 100644 index 0000000000..0e79cc3b1e --- /dev/null +++ b/challenge-199/zapwai/raku/ch-1.raku @@ -0,0 +1,15 @@ +my @list = (1,2,3,1,1,3); +#my @list=(1,2,3); +say "Input: (" ~ join(",", @list) ~ ")"; +my $cnt = 0 ; +my $str; +loop (my $this = 0; $this < @list.elems - 1; $this++) { + loop (my $that = $this + 1; $that < @list.elems; $that++) { + if @list[$this] == @list[$that] { + $cnt++; + $str ~= "($this, $that)\n"; + } + } +} +say "Output: $cnt"; +say $str if ($str); diff --git a/challenge-199/zapwai/raku/ch-2.raku b/challenge-199/zapwai/raku/ch-2.raku new file mode 100644 index 0000000000..2a4ebbcf09 --- /dev/null +++ b/challenge-199/zapwai/raku/ch-2.raku @@ -0,0 +1,15 @@ +my @list = (3,0,1,1,9,7); +my ($x, $y, $z) = (7, 2, 3); +my $cnt = 0; +my $str; +loop (my $i = 0; $i < @list.elems - 2; $i++) { + loop (my $j = $i + 1; $j < @list.elems - 1; $j++) { + loop (my $k = $j + 1; $k < @list.elems; $k++) { + if (abs(@list[$i] - @list[$j]) <= $x && abs(@list[$j] - @list[$k]) <= $y && abs(@list[$k] - @list[$i]) <= $z) { + $str ~= "("~join(",",@list[$i],@list[$j],@list[$k])~") \t [i:$i,j:$j,k:$k]\n"; + $cnt++; + } + }}} +say "Input: (" ~ join(",", @list) ~ ")"; +say "Output: $cnt"; +say chomp $str; -- cgit From 26ac484b813dc041459be1b1315a36df5a5105c0 Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Mon, 9 Jan 2023 23:57:27 +0800 Subject: pwc 199 --- challenge-199/steve-g-lynn/blog.txt | 1 + challenge-199/steve-g-lynn/perl/ch-1.sh | 3 +++ challenge-199/steve-g-lynn/perl/ch-2.pl | 17 +++++++++++++++++ challenge-199/steve-g-lynn/raku/ch-1.sh | 3 +++ challenge-199/steve-g-lynn/raku/ch-2.p6 | 13 +++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 challenge-199/steve-g-lynn/blog.txt create mode 100755 challenge-199/steve-g-lynn/perl/ch-1.sh create mode 100755 challenge-199/steve-g-lynn/perl/ch-2.pl create mode 100755 challenge-199/steve-g-lynn/raku/ch-1.sh create mode 100755 challenge-199/steve-g-lynn/raku/ch-2.p6 diff --git a/challenge-199/steve-g-lynn/blog.txt b/challenge-199/steve-g-lynn/blog.txt new file mode 100644 index 0000000000..742781ee5e --- /dev/null +++ b/challenge-199/steve-g-lynn/blog.txt @@ -0,0 +1 @@ +https://thiujiac.blogspot.com/2023/01/pwc-199.html diff --git a/challenge-199/steve-g-lynn/perl/ch-1.sh b/challenge-199/steve-g-lynn/perl/ch-1.sh new file mode 100755 index 0000000000..a10b38fdb9 --- /dev/null +++ b/challenge-199/steve-g-lynn/perl/ch-1.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +perl -MAlgorithm::Combinatorics=combinations -wl -e 'print scalar( grep { $ARGV[$_->[0]]==$ARGV[$_->[1]] } combinations [0 .. @ARGV-1], 2)' $@ diff --git a/challenge-199/steve-g-lynn/perl/ch-2.pl b/challenge-199/steve-g-lynn/perl/ch-2.pl new file mode 100755 index 0000000000..da37277085 --- /dev/null +++ b/challenge-199/steve-g-lynn/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env -S perl -wl + +use strict; +use Algorithm::Combinatorics qw(combinations); + +print &good_triplets( 7,2,3, 3,0,1,1,9,7); #4 +print &good_triplets( 0,0,1, 1,1,2,2,3); #0 + +sub good_triplets { + my ($x,$y,$z,@array)=@_; + + scalar( + grep {abs($array[$_->[0]]-$array[$_->[2]]) <= $z} + grep {abs($array[$_->[1]]-$array[$_->[2]]) <= $y} + grep {abs($array[$_->[0]]-$array[$_->[1]]) <= $x} + combinations [0 .. @array-1], 3 ); +} diff --git a/challenge-199/steve-g-lynn/raku/ch-1.sh b/challenge-199/steve-g-lynn/raku/ch-1.sh new file mode 100755 index 0000000000..e77b95c2a4 --- /dev/null +++ b/challenge-199/steve-g-lynn/raku/ch-1.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +raku -e '(0..@*ARGS-1).combinations(2).grep({@*ARGS[@_[0]]==@*ARGS[@_[1]]}).elems.say' $@ diff --git a/challenge-199/steve-g-lynn/raku/ch-2.p6 b/challenge-199/steve-g-lynn/raku/ch-2.p6 new file mode 100755 index 0000000000..c77942b412 --- /dev/null +++ b/challenge-199/steve-g-lynn/raku/ch-2.p6 @@ -0,0 +1,13 @@ +#!/usr/bin/env perl6 + +say &good-triplets( [3,0,1,1,9,7], 7,2,3); #4 +say &good-triplets( [1,1,2,2,3], 0,0,1); #0 + +sub good-triplets( @array,$x,$y,$z ) { + (0 .. @array-1) + .combinations(3) + .grep({abs(@array[@_[0]]-@array[@_[1]]) <= $x}) + .grep({abs(@array[@_[1]]-@array[@_[2]]) <= $y}) + .grep({abs(@array[@_[0]]=@array[@_[1]]) <= $z}) + .elems +} -- cgit From 47e5d4fe1a78c50e54d7884d0fec4c989fa2bce0 Mon Sep 17 00:00:00 2001 From: boldcaml Date: Mon, 9 Jan 2023 10:26:35 -0600 Subject: finish day 199 --- challenge-199/rawleyfowler/README.md | 4 ++-- challenge-199/rawleyfowler/raku/ch-1.raku | 10 ++++++++++ challenge-199/rawleyfowler/raku/ch-2.raku | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 challenge-199/rawleyfowler/raku/ch-1.raku create mode 100644 challenge-199/rawleyfowler/raku/ch-2.raku diff --git a/challenge-199/rawleyfowler/README.md b/challenge-199/rawleyfowler/README.md index 4d4b3f88f8..bdb7081cb4 100644 --- a/challenge-199/rawleyfowler/README.md +++ b/challenge-199/rawleyfowler/README.md @@ -1,5 +1,5 @@ ## How to run ```bash -raku raku/ch-1.raku '2 5 8 1' # Find gap pairs -raku raku/ch-2.raku '100' # Find primes +raku raku/ch-1.raku '1 2 3 1 1 3' # Find good pairs +raku raku/ch-2.raku '3 0 1 1 9 7' 7 2 3 # Find good triplets ``` diff --git a/challenge-199/rawleyfowler/raku/ch-1.raku b/challenge-199/rawleyfowler/raku/ch-1.raku new file mode 100644 index 0000000000..b8d22b8e08 --- /dev/null +++ b/challenge-199/rawleyfowler/raku/ch-1.raku @@ -0,0 +1,10 @@ +my @lst = @*ARGS[0].split: /\s/; +die 'No list provided' unless @lst.elems > 0; + +loop (my $i = 0; $i < @lst.elems; $i++) { + state $count = 0; + loop (my $j = $i + 1; $j < @lst.elems; $j++) { + $count++ if @lst[$i] == @lst[$j]; + } + say $count if $i + 1 == @lst.elems; +} diff --git a/challenge-199/rawleyfowler/raku/ch-2.raku b/challenge-199/rawleyfowler/raku/ch-2.raku new file mode 100644 index 0000000000..654f21d055 --- /dev/null +++ b/challenge-199/rawleyfowler/raku/ch-2.raku @@ -0,0 +1,16 @@ +use v6; +sub MAIN($lst, $x, $y, $z) { + my @lst = $lst.split(/\s/) || die 'Bad list passed'; + my $n = @lst.elems - 1; + my $count = 0; + for 0..$n -> $i { + for ($i+1)..$n -> $j { + for ($j+1)..$n -> $k { + if abs(@lst[$i] - @lst[$j]) <= $x && + abs(@lst[$j] - @lst[$k]) <= $y && + abs(@lst[$i] - @lst[$k]) <= $z { $count++ } + } + } + } + say $count; +} -- cgit From 81a52a81d32a7fa7853ba76e841f1c1bc81a1ebb Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 9 Jan 2023 12:10:19 -0500 Subject: DAJ 199.2 --- challenge-199/dave-jacoby/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-199/dave-jacoby/blog.txt diff --git a/challenge-199/dave-jacoby/blog.txt b/challenge-199/dave-jacoby/blog.txt new file mode 100644 index 0000000000..e790c62613 --- /dev/null +++ b/challenge-199/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/01/09/for-the-good-the-weekly-challenge-199.html -- cgit From 1d48c12e835eab4c2909f37f489f258e6c167663 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 9 Jan 2023 12:11:28 -0500 Subject: DAJ 199.3 --- challenge-199/dave-jacoby/perl/ch-2.pl | 1 - 1 file changed, 1 deletion(-) diff --git a/challenge-199/dave-jacoby/perl/ch-2.pl b/challenge-199/dave-jacoby/perl/ch-2.pl index 5275629dec..0eab4308bd 100644 --- a/challenge-199/dave-jacoby/perl/ch-2.pl +++ b/challenge-199/dave-jacoby/perl/ch-2.pl @@ -39,7 +39,6 @@ sub good_triplets ( $x, $y, $z, @array ) { } } - say join ' ', $x, $y, $z, '|', @array; return $out; } -- cgit From 2b52dd46834552ce48d7f2ee74433973bb1546ed Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 9 Jan 2023 20:18:39 +0100 Subject: Add solution 199 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-199/jeanluc2020/blog-1.txt | 1 + challenge-199/jeanluc2020/blog-2.txt | 1 + challenge-199/jeanluc2020/perl/ch-1.pl | 44 ++++++++++++++++++++++++++++ challenge-199/jeanluc2020/perl/ch-2.pl | 52 ++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 challenge-199/jeanluc2020/blog-1.txt create mode 100644 challenge-199/jeanluc2020/blog-2.txt create mode 100755 challenge-199/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-199/jeanluc2020/perl/ch-2.pl diff --git a/challenge-199/jeanluc2020/blog-1.txt b/challenge-199/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..5773d6ea73 --- /dev/null +++ b/challenge-199/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-199-1.html diff --git a/challenge-199/jeanluc2020/blog-2.txt b/challenge-199/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..b526ba3dd6 --- /dev/null +++ b/challenge-199/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-199-2.html diff --git a/challenge-199/jeanluc2020/perl/ch-1.pl b/challenge-199/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..5fe9804ff5 --- /dev/null +++ b/challenge-199/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-199/#TASK1 +# +# You are given a list of integers, @list. +# +# Write a script to find the total count of Good Pairs. +# +## A pair (i, j) is called good if list[i] == list[j] and i < j. +# +#################### +# +# solution: +# +# The solution to this problem is quite simple. Walk from the first element to +# the last with i, then walk from i to the last element with j, and take a note +# of the pair if list[i] == list[j]. Nothing too wild. + +use strict; +use warnings; + +# some examples +my @lists = ( + [1,2,3,1,1,3], + [1,2,3], + [1,1,1,1], + [], + [1,2,3,4,1,2,3,1,2,1] +); + +foreach my $list (@lists) { + find_good_pairs(@$list); +} + + +sub find_good_pairs { + my @list = @_; + my $count = 0; + foreach my $i (0..$#list) { + foreach my $j ($i+1..$#list) { + $count++ if $list[$i] == $list[$j]; + } + } + print "[" . join(",",@list) . "] returns $count\n"; +} diff --git a/challenge-199/jeanluc2020/perl/ch-2.pl b/challenge-199/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..5bb32a12e7 --- /dev/null +++ b/challenge-199/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-199/#TASK2 +# +# You are given an array of integers, @array and three integers $x,$y,$z. +# +# Write a script to find out total Good Triplets in the given array. +# +# A triplet array[i], array[j], array[k] is good if it satisfies the following conditions: +# +## a) 0 <= i < j < k <= n (size of given array) +## b) abs(array[i] - array[j]) <= x +## c) abs(array[j] - array[k]) <= y +## d) abs(array[i] - array[k]) <= z +# +##################################### +# +# solution: +# +# The solution to this problem is a bit similar to the first one where we have +# to walk the array with 2 variables; now we need three, and the check we have +# to do for each iteration is a bit more complicated. +# + + +use strict; +use warnings; + +# some examples + +my @examples = ( + [[3,0,1,1,9,7], 7, 2, 3], + [[1,1,2,2,3],0,0,1] +); + +foreach my $example (@examples) { + my ($list, $x, $y, $z) = @$example; + find_good_triplets($x, $y, $z, @$list); +} + +sub find_good_triplets { + my ($x, $y, $z, @list) = @_; + my $count = 0; + foreach my $i (0..$#list) { + foreach my $j ($i+1..$#list) { + foreach my $k ($j+1..$#list) { + $count++ if abs($list[$i]-$list[$j]) <= $x and abs($list[$j]-$list[$k]) <= $y and abs($list[$i]-$list[$k]) <= $z; + } + } + } + print "[" . join(",",@list) . "] returns $count\n"; +} + -- cgit From 4b3ae31959af409b55ae29e72b35371d81da6604 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 9 Jan 2023 20:24:38 +0000 Subject: Solutions for challenge #199 --- challenge-199/roger-bell-west/javascript/ch-1.js | 38 +++++++++++++ challenge-199/roger-bell-west/javascript/ch-2.js | 33 +++++++++++ challenge-199/roger-bell-west/kotlin/ch-1.kt | 38 +++++++++++++ challenge-199/roger-bell-west/kotlin/ch-2.kt | 35 ++++++++++++ challenge-199/roger-bell-west/lua/ch-1.lua | 39 +++++++++++++ challenge-199/roger-bell-west/lua/ch-2.lua | 33 +++++++++++ challenge-199/roger-bell-west/perl/ch-1.pl | 21 +++++++ challenge-199/roger-bell-west/perl/ch-2.pl | 27 +++++++++ challenge-199/roger-bell-west/postscript/ch-1.ps | 70 ++++++++++++++++++++++++ challenge-199/roger-bell-west/postscript/ch-2.ps | 68 +++++++++++++++++++++++ challenge-199/roger-bell-west/python/ch-1.py | 27 +++++++++ challenge-199/roger-bell-west/python/ch-2.py | 23 ++++++++ challenge-199/roger-bell-west/raku/ch-1.p6 | 19 +++++++ challenge-199/roger-bell-west/raku/ch-2.p6 | 25 +++++++++ challenge-199/roger-bell-west/ruby/ch-1.rb | 31 +++++++++++ challenge-199/roger-bell-west/ruby/ch-2.rb | 32 +++++++++++ challenge-199/roger-bell-west/rust/ch-1.rs | 32 +++++++++++ challenge-199/roger-bell-west/rust/ch-2.rs | 28 ++++++++++ challenge-199/roger-bell-west/tests.yaml | 50 +++++++++++++++++ 19 files changed, 669 insertions(+) create mode 100755 challenge-199/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-199/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-199/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-199/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-199/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-199/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-199/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-199/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-199/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-199/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-199/roger-bell-west/python/ch-1.py create mode 100755 challenge-199/roger-bell-west/python/ch-2.py create mode 100755 challenge-199/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-199/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-199/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-199/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-199/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-199/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-199/roger-bell-west/tests.yaml diff --git a/challenge-199/roger-bell-west/javascript/ch-1.js b/challenge-199/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..93218d5032 --- /dev/null +++ b/challenge-199/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,38 @@ +#! /usr/bin/node + +"use strict" + +function goodpairs(l) { + let c = 0; + let k = new Map(); + for (let i of l) { + if (k.has(i)) { + k.set(i, k.get(i) + 1); + } else { + k.set(i, 1); + } + } + for (let v of k.values()) { + c += v * (v - 1); + } + return c / 2; +} + +if (goodpairs([1, 2, 3, 1, 1, 3]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (goodpairs([1, 2, 3]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (goodpairs([1, 1, 1, 1]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-199/roger-bell-west/javascript/ch-2.js b/challenge-199/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..be44f6d4c2 --- /dev/null +++ b/challenge-199/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,33 @@ +#! /usr/bin/node + +"use strict" + +function goodtriplets(a, x, y, z) { + let c = 0; + for (let i = 0; i <= a.length - 3; i++) { + for (let j = i + 1; j <= a.length - 2; j++) { + if (Math.abs(a[i] - a[j]) <= x) { + for (let k = j + 1; k <= a.length - 1; k++) { + if (Math.abs(a[j] - a[k]) <= y && + Math.abs(a[i] - a[k]) <= z) { + c++; + } + } + } + } + } + return c; +} + +if (goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (goodtriplets([1, 1, 2, 2, 3], 0, 0, 1) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-199/roger-bell-west/kotlin/ch-1.kt b/challenge-199/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..c59e10195c --- /dev/null +++ b/challenge-199/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,38 @@ +fun goodpairs(l: List): Int { + var c = 0 + var k = mutableMapOf() + for (i in l) { + if (k.containsKey(i)) { + k[i] = k[i]!!+1 + } else { + k[i] = 1 + } + } + for (v in k.values) { + c += v * (v - 1) + } + return c / 2 +} + +fun main() { + + if (goodpairs(listOf(1, 2, 3, 1, 1, 3)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (goodpairs(listOf(1, 2, 3)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (goodpairs(listOf(1, 1, 1, 1)) == 6) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-199/roger-bell-west/kotlin/ch-2.kt b/challenge-199/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..65dc2cedf5 --- /dev/null +++ b/challenge-199/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,35 @@ +import kotlin.math.* + +fun goodtriplets(a: List, x: Int, y: Int, z: Int): Int { + var c = 0 + for (i in 0..a.size-3) { + for (j in i+1..a.size-2) { + if (abs(a[i] - a[j]) <= x) { + for (k in j+1..a.size-1) { + if (abs(a[j] - a[k]) <= y && + abs(a[i] - a[k]) <= z) { + c += 1 + } + } + } + } + } + return c +} + +fun main() { + + if (goodtriplets(listOf(3, 0, 1, 1, 9, 7), 7, 2, 3) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (goodtriplets(listOf(1, 1, 2, 2, 3), 0, 0, 1) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-199/roger-bell-west/lua/ch-1.lua b/challenge-199/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..3931fe1154 --- /dev/null +++ b/challenge-199/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,39 @@ +#! /usr/bin/lua + +function goodpairs(l) + local c = 0 + local k = {} + for dummy, i in ipairs(l) do + if k[i] == nil then + k[i] = 1 + else + k[i] = k[i] + 1 + end + end + for dummy, v in ipairs(k) do + c = c + v * (v - 1) + end + return c / 2 +end + +if goodpairs({1, 2, 3, 1, 1, 3}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if goodpairs({1, 2, 3}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if goodpairs({1, 1, 1, 1}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-199/roger-bell-west/lua/ch-2.lua b/challenge-199/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..63d4cad318 --- /dev/null +++ b/challenge-199/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,33 @@ +#! /usr/bin/lua + +function goodtriplets(a, x, y, z) + local c = 0 + for i = 1, #a - 2 do + for j = i + 1, #a - 1 do + if math.abs(a[i] - a[j]) <= x then + for k = j + 1, #a do + if math.abs(a[j] - a[k]) <= y and + math.abs(a[i] - a[k]) <= z then + c = c + 1 + end + end + end + end + end + return c +end + +if goodtriplets({3, 0, 1, 1, 9, 7}, 7, 2, 3) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if goodtriplets({1, 1, 2, 2, 3}, 0, 0, 1) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-199/roger-bell-west/perl/ch-1.pl b/challenge-199/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..6c8febdd0f --- /dev/null +++ b/challenge-199/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(goodpairs([1, 2, 3, 1, 1, 3]), 4, 'example 1'); +is(goodpairs([1, 2, 3]), 0, 'example 2'); +is(goodpairs([1, 1, 1, 1]), 6, 'example 3'); + +sub goodpairs($l) { + my $c = 0; + my %k; + map {$k{$_}++} @{$l}; + foreach my $v (values %k) { + $c += $v * ($v-1); + } + return $c / 2; +} diff --git a/challenge-199/roger-bell-west/perl/ch-2.pl b/challenge-199/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..ac81a4f099 --- /dev/null +++ b/challenge-199/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,27 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3), 4, 'example 1'); +is(goodtriplets([1, 1, 2, 2, 3], 0, 0, 1), 0, 'example 2'); + +sub goodtriplets($a, $x, $y, $z) { + my $c = 0; + foreach my $i (0..$#{$a} - 2) { + foreach my $j ($i+1..$#{$a} - 1) { + if (abs($a->[$i] - $a->[$j]) <= $x) { + foreach my $k ($j+1..$#{$a}) { + if (abs($a->[$j] - $a->[$k]) <= $y && + abs($a->[$i] - $a->[$k]) <= $z) { + $c++; + } + } + } + } + } + return $c; +} diff --git a/challenge-199/roger-bell-west/postscript/ch-1.ps b/challenge-199/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..15731eb3b4 --- /dev/null +++ b/challenge-199/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,70 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/values { % dict -> array of dict values + [ exch + { + exch pop + } forall + ] +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/goodpairs { + 1 dict begin + /k 0 dict def + { + dup k exch known { + dup k exch get + } { + 0 + } ifelse + k 3 1 roll 1 add put + } forall + 0 + k values { + dup 1 sub mul add + } forall + 2 idiv + end +} bind def + +(goodpairs) test.start +[1 2 3 1 1 3] goodpairs 4 eq test +[1 2 3] goodpairs 0 eq test +[1 1 1 1] goodpairs 6 eq test +test.end diff --git a/challenge-199/roger-bell-west/postscript/ch-2.ps b/challenge-199/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..dcc459d9bc --- /dev/null +++ b/challenge-199/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,68 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + + +% end included library code + +/goodtriplets { + 7 dict begin + /z exch def + /y exch def + /x exch def + /a exch def + 0 + 0 1 a length 3 sub { + /i exch def + i 1 add 1 a length 2 sub { + /j exch def + a i get a j get sub abs x le { + j 1 add 1 a length 1 sub { + /k exch def + a j get a k get sub abs y le + a i get a k get sub abs z le and { + 1 add + } if + } for + } if + } for + } for + end + +} bind def + +(goodtriplets) test.start +[3 0 1 1 9 7] 7 2 3 goodtriplets 4 eq test +[1 1 2 2 3] 0 0 1 goodtriplets 0 eq test +test.end diff --git a/challenge-199/roger-bell-west/python/ch-1.py b/challenge-199/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..1f35b4fc87 --- /dev/null +++ b/challenge-199/roger-bell-west/python/ch-1.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +import unittest + +from collections import defaultdict + +def goodpairs(l): + c = 0 + k = defaultdict(lambda: 0) + for i in l: + k[i] += 1 + for v in k.values(): + c += v * (v - 1) + return c / 2 + +class TestGoodpairs(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(goodpairs([1, 2, 3, 1, 1, 3]), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(goodpairs([1, 2, 3]), 0, 'example 2') + + def test_ex3(self): + self.assertEqual(goodpairs([1, 1, 1, 1]), 6, 'example 3') + +unittest.main() diff --git a/challenge-199/roger-bell-west/python/ch-2.py b/challenge-199/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..b96dac98dc --- /dev/null +++ b/challenge-199/roger-bell-west/python/ch-2.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 + +import unittest + +def goodtriplets(a, x, y, z): + c = 0 + for i in range(len(a) - 2): + for j in range(i + 1, len(a) - 1): + if abs(a[i] - a[j]) <= x: + for k in range(j + 1, len(a)): + if abs(a[j] - a[k]) <= y and abs(a[i] - a[k]) <= z: + c += 1 + return c + +class TestGoodtriplets(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(goodtriplets([1, 1, 2, 2, 3], 0, 0, 1), 0, 'example 2') + +unittest.main() diff --git a/challenge-199/roger-bell-west/raku/ch-1.p6 b/challenge-199/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..9bcf412b53 --- /dev/null +++ b/challenge-199/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(goodpairs([1, 2, 3, 1, 1, 3]), 4, 'example 1'); +is(goodpairs([1, 2, 3]), 0, 'example 2'); +is(goodpairs([1, 1, 1, 1]), 6, 'example 3'); + +sub goodpairs(@l) { + my $c = 0; + my %k; + map {%k{$_}++}, @l; + for %k.values() -> $v { + $c += $v * ($v-1); + } + return $c / 2; +} diff --git a/challenge-199/roger-bell-west/raku/ch-2.p6 b/challenge-199/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..47fb60b20e --- /dev/null +++ b/challenge-199/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3), 4, 'example 1'); +is(goodtriplets([1, 1, 2, 2, 3], 0, 0, 1), 0, 'example 2'); + +sub goodtriplets(@a, $x, $y, $z) { + my $c = 0; + for (0..@a.end - 2) -> $i { + for ($i+1..@a.end - 1) -> $j { + if (abs(@a[$i] - @a[$j]) <= $x) { + for ($j+1..@a.end) -> $k { + if (abs(@a[$j] - @a[$k]) <= $y && + abs(@a[$i] - @a[$k]) <= $z) { + $c++; + } + } + } + } + } + return $c; +} diff --git a/challenge-199/roger-bell-west/ruby/ch-1.rb b/challenge-199/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..53cc7318bd --- /dev/null +++ b/challenge-199/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def goodpairs(l) + c = 0 + k = Hash.new(0) + l.each do |i| + k[i] += 1 + end + k.values.each do |v| + c += v * (v - 1) + end + return c / 2 +end + +class TestGoodpairs < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, goodpairs([1, 2, 3, 1, 1, 3])) + end + + def test_ex2 + assert_equal(0, goodpairs([1, 2, 3])) + end + + def test_ex3 + assert_equal(6, goodpairs([1, 1, 1, 1])) + end + +end diff --git a/challenge-199/roger-bell-west/ruby/ch-2.rb b/challenge-199/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..f016291568 --- /dev/null +++ b/challenge-199/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,32 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def goodtriplets(a, x, y, z) + c = 0 + 0.upto(a.length - 3) do |i| + (i + 1).upto(a.length - 2) do |j| + if (a[i] - a[j]).abs <= x then + (j + 1).upto(a.length - 1) do |k| + if (a[j] - a[k]).abs <= y && + (a[i] - a[k]).abs <= z then + c += 1 + end + end + end + end + end + return c +end + +class TestGoodtriplets < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, goodtriplets([3, 0, 1, 1, 9, 7], 7, 2, 3)) + end + + def test_ex2 + assert_equal(0, goodtriplets([1, 1, 2, 2, 3], 0, 0, 1)) + end + +end diff --git a/challenge-199/roger-bell-west/rust/ch-1.rs b/challenge-199/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..8c7f28ce1f --- /dev/null +++ b/challenge-199/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,32 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashMap; + +#[test] +fn test_ex1() { + assert_eq!(goodpairs(vec![1, 2, 3, 1, 1, 3]), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(goodpairs(vec![1, 2, 3]), 0); +} + +#[test] +fn test_ex3() { + assert_eq!(goodpairs(vec![1, 1, 1, 1]), 6); +} + +fn goodpairs(l: Vec) -> usize { + let mut c = 0; + let mut k: HashMap = HashMap::new(); + for i in l { + let en = k.entry(i).or_insert(0); + *en += 1; + } + for v in k.values() { + c += v * (v - 1); + } + c / 2 +} diff --git a/challenge-199/roger-bell-west/rust/ch-2.rs b/challenge-199/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..83c624d79a --- /dev/null +++ b/challenge-199/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,28 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(goodtriplets(vec![3, 0, 1, 1, 9, 7], 7, 2, 3), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(goodtriplets(vec![1, 1, 2, 2, 3], 0, 0, 1), 0); +} + +fn goodtriplets(a: Vec, x: isize, y: isize, z: isize) -> usize { + let mut c = 0; + for i in 0..=a.len() - 3 { + for j in (i + 1)..=a.len() - 2 { + if (a[i] - a[j]).abs() <= x { + for k in (j + 1)..=a.len() - 1 { + if (a[j] - a[k]).abs() <= y && (a[i] - a[k]).abs() <= z { + c += 1; + } + } + } + } + } + c +} diff --git a/challenge-199/roger-bell-west/tests.yaml b/challenge-199/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..6ca095a9bc --- /dev/null +++ b/challenge-199/roger-bell-west/tests.yaml @@ -0,0 +1,50 @@ +--- +ch-1: + - function: goodpairs + arguments: + - 1 + - 2 + - 3 + - 1 + - 1 + - 3 + result: 4 + - function: goodpairs + arguments: + - 1 + - 2 + - 3 + result: 0 + - function: goodpairs + arguments: + - 1 + - 1 + - 1 + - 1 + result: 6 +ch-2: + - function: goodtriplets + arguments: + - - 3 + - 0 + - 1 + - 1 + - 9 + - 7 + - 7 + - 2 + - 3 + multiarg: true + result: 4 + - function: goodtriplets + arguments: + - - 1 + - 1 + - 2 + - 2 + - 3 + - 0 + - 0 + - 1 + multiarg: true + result: 0 -- cgit From 2ea55eb9278c406022a1ac0123f3a9d78b65d120 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 9 Jan 2023 23:51:30 +0100 Subject: Add solutions to 199: Good Pairs & Good Triplets by E. Choroba --- challenge-199/e-choroba/perl/ch-1.pl | 21 +++++++++++++++++++++ challenge-199/e-choroba/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100755 challenge-199/e-choroba/perl/ch-1.pl create mode 100755 challenge-199/e-choroba/perl/ch-2.pl diff --git a/challenge-199/e-choroba/perl/ch-1.pl b/challenge-199/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..57a30da381 --- /dev/null +++ b/challenge-199/e-choroba/perl/ch-1.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw{ signatures }; + +use List::Util qw{ sum }; + +sub good_pairs ($list) { + my %counts; + ++$counts{$_} for @$list; + return sum(map $_ * ($_ - 1) / 2, values %counts) // 0 +} + +use Test::More tests => 3 + 2; + +is good_pairs([1, 2, 3, 1, 1, 3]), 4, 'Example 1'; +is good_pairs([1, 2, 3]), 0, 'Example 2'; +is good_pairs([1, 1, 1, 1]), 6, 'Example 3'; + +is good_pairs([1, 2, 1, 2, 1, 2, 1, 2, 1]), 16, '5+4'; +is good_pairs([]), 0, 'Empty'; diff --git a/challenge-199/e-choroba/perl/ch-2.pl b/challenge-199/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..c59cba3414 --- /dev/null +++ b/challenge-199/e-choroba/perl/ch-2.pl @@ -0,0 +1,28 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub good_triplets ($arr, $x, $y, $z) { + # Optimization: abs can't be < 0. + return 0 if grep $_ < 0, $x, $y, $z; + + my $c = 0; + for my $i (0 .. $#$arr - 2) { + for my $j ($i + 1 .. $#$arr - 1) { + next unless abs($arr->[$i] - $arr->[$j]) <= $x; + for my $k ($j + 1 .. $#$arr) { + ++$c if abs($arr->[$j] - $arr->[$k]) <= $y + && abs($arr->[$i] - $arr->[$k]) <= $z; + } + } + } + return $c +} + +use Test::More tests => 2 + 1; + +is good_triplets([3, 0, 1, 1, 9, 7], 7, 2, 3), 4, 'Example 1'; +is good_triplets([1, 1, 2, 2, 3], 0, 0, 1), 0, 'Example 2'; + +is good_triplets([map int rand 100, 1 .. 1000], 0, 0, -1), 0, 'Negative'; -- cgit From bf34915beed9bd1493064d97705413d9e39229f3 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 9 Jan 2023 23:16:59 +0000 Subject: Adding tests and fix stupid typo bugs. Small optimization --- challenge-199/perlboy1967/perl/ch-1.pl | 7 +++++-- challenge-199/perlboy1967/perl/ch-2.pl | 9 ++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/challenge-199/perlboy1967/perl/ch-1.pl b/challenge-199/perlboy1967/perl/ch-1.pl index a1389d07e4..868028af41 100755 --- a/challenge-199/perlboy1967/perl/ch-1.pl +++ b/challenge-199/perlboy1967/perl/ch-1.pl @@ -26,13 +26,15 @@ use Test::More; use Test::Deep qw(cmp_deeply); -sub goodPairs { +sub goodPairs (@) { my $ar = []; - for my $i (0 .. scalar(@_) - 1) { + + for my $i (0 .. scalar(@_) - 2) { for my $j ($i + 1 .. scalar(@_) - 1) { push(@$ar,[$i,$j]) if ($_[$i] == $_[$j] and $i < $j); } } + return $ar; } @@ -40,5 +42,6 @@ sub goodPairs { cmp_deeply(goodPairs(1,2,3,1,1,3),[[0,3],[0,4],[2,5],[3,4]]); cmp_deeply(goodPairs(1,2,3),[]); cmp_deeply(goodPairs(1,1,1,1),[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]); +cmp_deeply(goodPairs(1,2,1,2),[[0,2],[1,3]]); done_testing; diff --git a/challenge-199/perlboy1967/perl/ch-2.pl b/challenge-199/perlboy1967/perl/ch-2.pl index 523c272b95..07c63ec12d 100755 --- a/challenge-199/perlboy1967/perl/ch-2.pl +++ b/challenge-199/perlboy1967/perl/ch-2.pl @@ -35,9 +35,10 @@ sub goodTriplets ($$$\@) { my ($x,$y,$z,$arIn) = @_; my $arOut = []; - for my $i (0 .. scalar(@_) - 1) { - for my $j ($i + 1 .. scalar(@_) - 1) { - for my $k ($j + 1 .. scalar(@_) - 1) { + + for my $i (0 .. scalar(@$arIn) - 3) { + for my $j ($i + 1 .. scalar(@$arIn) - 2) { + for my $k ($j + 1 .. scalar(@$arIn) - 1) { push(@$arOut,[$$arIn[$i],$$arIn[$j],$$arIn[$k]]) if (abs($$arIn[$i] - $$arIn[$j]) <= $x and abs($$arIn[$j] - $$arIn[$k]) <= $y and @@ -54,5 +55,7 @@ cmp_deeply(goodTriplets(7,2,3,@{[3,0,1,1,9,7]}), [[3,0,1],[3,0,1],[3,1,1],[0,1,1]]); cmp_deeply(goodTriplets(0,0,1,@{[1,1,2,2,3]}), []); +cmp_deeply(goodTriplets(3,2,1,@{[1,4,2,0,3]}), + [[1,4,2],[1,2,0],[4,2,3]]); done_testing; -- cgit From 792be6f83093ba0c4588346ec7dc7c68eadc5d36 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Tue, 10 Jan 2023 00:00:55 +0000 Subject: Layout of code --- challenge-199/perlboy1967/perl/ch-1.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/challenge-199/perlboy1967/perl/ch-1.pl b/challenge-199/perlboy1967/perl/ch-1.pl index 868028af41..c3249772aa 100755 --- a/challenge-199/perlboy1967/perl/ch-1.pl +++ b/challenge-199/perlboy1967/perl/ch-1.pl @@ -31,7 +31,8 @@ sub goodPairs (@) { for my $i (0 .. scalar(@_) - 2) { for my $j ($i + 1 .. scalar(@_) - 1) { - push(@$ar,[$i,$j]) if ($_[$i] == $_[$j] and $i < $j); + push(@$ar,[$i,$j]) + if ($_[$i] == $_[$j] and $i < $j); } } -- cgit From 766e0203251bfd83ada482de92d6fcbea736adc9 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 9 Jan 2023 21:49:22 -0600 Subject: Solve PWC199 --- challenge-199/wlmb/blog.txt | 2 ++ challenge-199/wlmb/perl/ch-1.pl | 12 ++++++++++++ challenge-199/wlmb/perl/ch-1a.pl | 16 ++++++++++++++++ challenge-199/wlmb/per