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