From 54275549bdb80e9af63fdfb040b6ef7def65b181 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sat, 14 Jan 2023 19:57:48 -0500 Subject: initial commit --- challenge-199/adam-russell/blog.txt | 1 + challenge-199/adam-russell/blog1.txt | 1 + challenge-199/adam-russell/perl/ch-1.pl | 24 ++++++++++++++++++++++ challenge-199/adam-russell/perl/ch-2.pl | 34 ++++++++++++++++++++++++++++++++ challenge-199/adam-russell/prolog/ch-1.p | 10 ++++++++++ challenge-199/adam-russell/prolog/ch-2.p | 14 +++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 challenge-199/adam-russell/blog.txt create mode 100644 challenge-199/adam-russell/blog1.txt create mode 100644 challenge-199/adam-russell/perl/ch-1.pl create mode 100644 challenge-199/adam-russell/perl/ch-2.pl create mode 100644 challenge-199/adam-russell/prolog/ch-1.p create mode 100644 challenge-199/adam-russell/prolog/ch-2.p 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]. -- cgit