diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-10 18:36:21 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-10 18:36:21 +0000 |
| commit | 79c9fc30ab7cad1bd61c3b82a60b0a4d7b174ae7 (patch) | |
| tree | a1d28b851c5b50fc511e94e3577dc5536f24b200 /challenge-199 | |
| parent | 8e6b8ea13f6db2802d1355c821e51706f7e8ccca (diff) | |
| parent | 2b52dd46834552ce48d7f2ee74433973bb1546ed (diff) | |
| download | perlweeklychallenge-club-79c9fc30ab7cad1bd61c3b82a60b0a4d7b174ae7.tar.gz perlweeklychallenge-club-79c9fc30ab7cad1bd61c3b82a60b0a4d7b174ae7.tar.bz2 perlweeklychallenge-club-79c9fc30ab7cad1bd61c3b82a60b0a4d7b174ae7.zip | |
Merge pull request #7392 from jeanluc2020/jeanluc-199
Add solution 199
Diffstat (limited to 'challenge-199')
| -rw-r--r-- | challenge-199/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-199/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-199/jeanluc2020/perl/ch-1.pl | 44 | ||||
| -rwxr-xr-x | challenge-199/jeanluc2020/perl/ch-2.pl | 52 |
4 files changed, 98 insertions, 0 deletions
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"; +} + |
