diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-15 21:55:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-15 21:55:04 +0000 |
| commit | 37c17e5a81edc20d94dc3cc85451943a1c263c28 (patch) | |
| tree | 189cc91e441343436acb95b6853b19a78239a01d | |
| parent | 4269f93afde28ae12acb9bf2b2242a3132bfcdd4 (diff) | |
| parent | 54275549bdb80e9af63fdfb040b6ef7def65b181 (diff) | |
| download | perlweeklychallenge-club-37c17e5a81edc20d94dc3cc85451943a1c263c28.tar.gz perlweeklychallenge-club-37c17e5a81edc20d94dc3cc85451943a1c263c28.tar.bz2 perlweeklychallenge-club-37c17e5a81edc20d94dc3cc85451943a1c263c28.zip | |
Merge pull request #7420 from adamcrussell/challenge-199
initial commit
| -rw-r--r-- | challenge-199/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-199/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-199/adam-russell/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-199/adam-russell/perl/ch-2.pl | 34 | ||||
| -rw-r--r-- | challenge-199/adam-russell/prolog/ch-1.p | 10 | ||||
| -rw-r--r-- | challenge-199/adam-russell/prolog/ch-2.p | 14 |
6 files changed, 84 insertions, 0 deletions
diff --git a/challenge-199/adam-russell/blog.txt b/challenge-199/adam-russell/blog.txt new file mode 100644 index 0000000000..296cd3ffff --- /dev/null +++ b/challenge-199/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2023/01/15 diff --git a/challenge-199/adam-russell/blog1.txt b/challenge-199/adam-russell/blog1.txt new file mode 100644 index 0000000000..911660e27c --- /dev/null +++ b/challenge-199/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2023/01/15 diff --git a/challenge-199/adam-russell/perl/ch-1.pl b/challenge-199/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..dbf90c3720 --- /dev/null +++ b/challenge-199/adam-russell/perl/ch-1.pl @@ -0,0 +1,24 @@ +use v5.36; +## +# 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. +## +sub good_pairs{ + my(@numbers) = @_; + my @pairs; + do{ + my $i = $_; + do{ + my $j = $_; + push @pairs, [$i, $j] if $numbers[$i] == $numbers[$j] && $i < $j; + } for 0 .. @numbers - 1; + } for 0 .. @numbers - 1; + return 0 + @pairs; +} + +MAIN:{ + say good_pairs 1, 2, 3, 1, 1, 3; + say good_pairs 1, 2, 3; + say good_pairs 1, 1, 1, 1; +} diff --git a/challenge-199/adam-russell/perl/ch-2.pl b/challenge-199/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..3d7e7c662e --- /dev/null +++ b/challenge-199/adam-russell/perl/ch-2.pl @@ -0,0 +1,34 @@ +use v5.36; +## +# 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 +## +use Math::Combinatorics; +sub good_triplets{ + my($numbers, $x, $y, $z) = @_; + my $combinations = Math::Combinatorics->new(count => 3, data => [0 .. @{$numbers} - 1]); + my @combination = $combinations->next_combination; + my @good_triplets; + { + my($s, $t, $u) = @combination; + unless($s >= $t || $t >= $u || $s >= $u){ + push @good_triplets, [@{$numbers}[$s, $t, $u]] if(abs($numbers->[$s] - $numbers->[$t]) <= $x && + abs($numbers->[$t] - $numbers->[$u]) <= $y && + abs($numbers->[$s] - $numbers->[$u]) <= $z); + + } + @combination = $combinations->next_combination; + redo if @combination; + } + return 0 + @good_triplets; +} + +MAIN:{ + say good_triplets([3, 0, 1, 1, 9, 7], 7, 2, 3); + say good_triplets([1, 1, 2, 2, 3], 0, 0, 1); +} diff --git a/challenge-199/adam-russell/prolog/ch-1.p b/challenge-199/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..4cf3a570e5 --- /dev/null +++ b/challenge-199/adam-russell/prolog/ch-1.p @@ -0,0 +1,10 @@ +good_pair(Numbers, Pair):- + length(Numbers, L), + fd_domain(I, 1, L), + fd_domain(J, 1, L), + I #<# J, + fd_labeling([I, J]), + fd_element(I, Numbers, Ith), + fd_element(J, Numbers, Jth), + Ith #= Jth, + Pair = [I, J]. diff --git a/challenge-199/adam-russell/prolog/ch-2.p b/challenge-199/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..4c43f766e8 --- /dev/null +++ b/challenge-199/adam-russell/prolog/ch-2.p @@ -0,0 +1,14 @@ +good_triplet(Numbers, X, Y, Z, Triplet):- + length(Numbers, I), + fd_domain(S, 1, I), + fd_domain(T, 1, I), + fd_domain(U, 1, I), + S #<# T, T #<# U, + fd_labeling([S, T, U]), + fd_element(S, Numbers, Sth), + fd_element(T, Numbers, Tth), + fd_element(U, Numbers, Uth), + Ast is abs(Sth - Tth), Ast #=<# X, + Atu is abs(Tth - Uth), Atu #=<# Y, + Asu is abs(Sth - Uth), Asu #=<# Z, + Triplet = [Sth, Tth, Uth]. |
