diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-13 18:22:44 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-13 18:22:44 +0000 |
| commit | 92c4e301ac64d081cf189a7bc1931c5b0c715fd3 (patch) | |
| tree | d6f426e748935b94f4247cb61bd14e9bf5b393e2 | |
| parent | 69e5455842b6434bcd932c635c684c202ff57db6 (diff) | |
| parent | d18742a481f56afe01099d0a3252810bfbfef65c (diff) | |
| download | perlweeklychallenge-club-92c4e301ac64d081cf189a7bc1931c5b0c715fd3.tar.gz perlweeklychallenge-club-92c4e301ac64d081cf189a7bc1931c5b0c715fd3.tar.bz2 perlweeklychallenge-club-92c4e301ac64d081cf189a7bc1931c5b0c715fd3.zip | |
Merge pull request #7721 from pauloscustodio/master
Add Perl solution
| -rw-r--r-- | challenge-199/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/perl/ch-1.pl | 57 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/perl/ch-2.pl | 56 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/t/test-2.yaml | 10 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/basic/ch-1.bas | 1 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/forth/ch-1.fs | 1 | ||||
| -rw-r--r-- | challenge-200/paulo-custodio/perl/ch-2.pl | 2 |
8 files changed, 140 insertions, 4 deletions
diff --git a/challenge-199/paulo-custodio/Makefile b/challenge-199/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-199/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-199/paulo-custodio/perl/ch-1.pl b/challenge-199/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..bb9dbea905 --- /dev/null +++ b/challenge-199/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +# Challenge 199 +# +# 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. +# +# +# Example 1 +# +# Input: @list = (1,2,3,1,1,3) +# Output: 4 +# +# There are 4 good pairs found as below: +# (0,3) +# (0,4) +# (3,4) +# (2,5) +# +# Example 2 +# +# Input: @list = (1,2,3) +# Output: 0 +# +# Example 3 +# +# Input: @list = (1,1,1,1) +# Output: 6 +# +# Good pairs are below: +# (0,1) +# (0,2) +# (0,3) +# (1,2) +# (1,3) +# (2,3) + +use Modern::Perl; + +sub count_good_pairs { + my(@in) = @_; + my $count = 0; + for my $i (0..$#in-1) { + for my $j ($i+1..$#in) { + $count++ if $in[$i] == $in[$j]; + } + } + return $count; +} + +say count_good_pairs(@ARGV); diff --git a/challenge-199/paulo-custodio/perl/ch-2.pl b/challenge-199/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..12212b4ff2 --- /dev/null +++ b/challenge-199/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# Challenge 199 +# +# 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 +# +# Example 1 +# +# Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +# Output: 4 +# +# Good Triplets are as below: +# (3,0,1) where (i=0, j=1, k=2) +# (3,0,1) where (i=0, j=1, k=3) +# (3,1,1) where (i=0, j=2, k=3) +# (0,1,1) where (i=1, j=2, k=3) +# +# Example 2 +# +# Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 +# Output: 0 + +use Modern::Perl; + +sub count_good_triplets { + my(@in) = @_; + my $z = pop @in; + my $y = pop @in; + my $x = pop @in; + my $count = 0; + for my $i (0..$#in-2) { + for my $j ($i+1..$#in-1) { + for my $k ($j+1..$#in) { + $count++ if (abs($in[$i] - $in[$j]) <= $x && + abs($in[$j] - $in[$k]) <= $y && + abs($in[$i] - $in[$k]) <= $z); + } + } + } + return $count; +} + +say count_good_triplets(@ARGV); diff --git a/challenge-199/paulo-custodio/t/test-1.yaml b/challenge-199/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..634e643dc3 --- /dev/null +++ b/challenge-199/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 3 1 1 3 + input: + output: 4 +- setup: + cleanup: + args: 1 2 3 + input: + output: 0 +- setup: + cleanup: + args: 1 1 1 1 + input: + output: 6 diff --git a/challenge-199/paulo-custodio/t/test-2.yaml b/challenge-199/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..67c4762796 --- /dev/null +++ b/challenge-199/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 3 0 1 1 9 7 7 2 3 + input: + output: 4 +- setup: + cleanup: + args: 1 1 2 2 3 0 0 1 + input: + output: 0 diff --git a/challenge-200/paulo-custodio/basic/ch-1.bas b/challenge-200/paulo-custodio/basic/ch-1.bas index ae410376dd..ca30fbaa71 100644 --- a/challenge-200/paulo-custodio/basic/ch-1.bas +++ b/challenge-200/paulo-custodio/basic/ch-1.bas @@ -69,4 +69,3 @@ end sub dim nums() as integer collect_args nums() slices nums() - diff --git a/challenge-200/paulo-custodio/forth/ch-1.fs b/challenge-200/paulo-custodio/forth/ch-1.fs index d01f35cbdb..daec4a57c6 100644 --- a/challenge-200/paulo-custodio/forth/ch-1.fs +++ b/challenge-200/paulo-custodio/forth/ch-1.fs @@ -67,4 +67,3 @@ CREATE nums 256 CELLS ALLOT collect_args print_slices BYE - diff --git a/challenge-200/paulo-custodio/perl/ch-2.pl b/challenge-200/paulo-custodio/perl/ch-2.pl index 1147f45cb4..29b49eb2a3 100644 --- a/challenge-200/paulo-custodio/perl/ch-2.pl +++ b/challenge-200/paulo-custodio/perl/ch-2.pl @@ -78,5 +78,3 @@ sub draw_number { } draw_number(shift || 0); - - |
