diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-20 13:29:08 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-20 13:29:08 +0100 |
| commit | 3b01d861087c61577f5553751fcfca2fb105869f (patch) | |
| tree | 6a00938eb121cfb1ffc2443bcad9dc95253d3b88 | |
| parent | 7dbb1abf788a19ad3dfbba2053a5248f6f9b2f49 (diff) | |
| download | perlweeklychallenge-club-3b01d861087c61577f5553751fcfca2fb105869f.tar.gz perlweeklychallenge-club-3b01d861087c61577f5553751fcfca2fb105869f.tar.bz2 perlweeklychallenge-club-3b01d861087c61577f5553751fcfca2fb105869f.zip | |
Add Perl solution to challenge 234
| -rw-r--r-- | challenge-234/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/perl/ch-2.pl | 49 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 127 insertions, 0 deletions
diff --git a/challenge-234/paulo-custodio/Makefile b/challenge-234/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-234/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-234/paulo-custodio/perl/ch-1.pl b/challenge-234/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..5f8b706d15 --- /dev/null +++ b/challenge-234/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# Challenge 234 +# +# Task 1: Common Characters +# Submitted by: Mohammad S Anwar +# +# You are given an array of words made up of alphabetic characters only. +# +# Write a script to return all alphabetic characters that show up in all words +# including duplicates. +# Example 1 +# +# Input: @words = ("java", "javascript", "julia") +# Output: ("j", "a") +# +# Example 2 +# +# Input: @words = ("bella", "label", "roller") +# Output: ("e", "l", "l") +# +# Example 3 +# +# Input: @words = ("cool", "lock", "cook") +# Output: ("c", "o") + +use Modern::Perl; +use List::Util 'all'; + +my @words = @ARGV; +say join " ", find_common_chars(@words); + +sub find_common_chars { + my(@words) = @_; + my @result; + + while (all {$_ ne ''} @words) { + my $ch = substr($words[0],0,1); + my $count; + for (@words) { + $count += s/$ch//i; + } + push @result, $ch if $count == @words; + } + return @result; +} diff --git a/challenge-234/paulo-custodio/perl/ch-2.pl b/challenge-234/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b4cf19879d --- /dev/null +++ b/challenge-234/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# Challenge 234 +# +# Task 2: Unequal Triplets +# Submitted by: Mohammad S Anwar +# +# You are given an array of positive integers. +# +# Write a script to find the number of triplets (i, j, k) that satisfies +# num[i] != num[j], num[j] != num[k] and num[k] != num[i]. +# Example 1 +# +# Input: @ints = (4, 4, 2, 4, 3) +# Ouput: 3 +# +# (0, 2, 4) because 4 != 2 != 3 +# (1, 2, 4) because 4 != 2 != 3 +# (2, 3, 4) because 2 != 4 != 3 +# +# Example 2 +# +# Input: @ints = (1, 1, 1, 1, 1) +# Ouput: 0 +# +# Example 3 +# +# Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +# Output: 28 +# +# triplets of 1, 4, 7 = 3x2×2 = 12 combinations +# triplets of 1, 4, 10 = 3×2×1 = 6 combinations +# triplets of 4, 7, 10 = 2×2×1 = 4 combinations +# triplets of 1, 7, 10 = 3x2x1 = 6 combinations + +use Modern::Perl; + +my @nums = @ARGV; +my $count = 0; +for my $i (0 .. $#nums-2) { + for my $j ($i+1 .. $#nums-1) { + for my $k ($j+1 .. $#nums) { + $count++ if $nums[$i] != $nums[$j] && + $nums[$j] != $nums[$k] && + $nums[$k] != $nums[$i]; + } + } +} +say $count; diff --git a/challenge-234/paulo-custodio/t/test-1.yaml b/challenge-234/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ba3da310b1 --- /dev/null +++ b/challenge-234/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: java javascript julia + input: + output: j a +- setup: + cleanup: + args: bella label roller + input: + output: e l l +- setup: + cleanup: + args: cool lock cook + input: + output: c o diff --git a/challenge-234/paulo-custodio/t/test-2.yaml b/challenge-234/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ce2a88f237 --- /dev/null +++ b/challenge-234/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 4 4 2 4 3 + input: + output: 3 +- setup: + cleanup: + args: 1 1 1 1 1 + input: + output: 0 +- setup: + cleanup: + args: 4 7 1 10 7 4 1 1 + input: + output: 28 |
