aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-08-15 10:37:16 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-08-15 10:37:16 +0100
commit03d3d64790298ccfbcc165ae4ddd92447eac5a5a (patch)
treedd03e21a7d2cd6a7c357ff2edef59f9659d1c2cf
parentf9f6f58167b8be5dddc9e15c469f5083b1cae42e (diff)
downloadperlweeklychallenge-club-03d3d64790298ccfbcc165ae4ddd92447eac5a5a.tar.gz
perlweeklychallenge-club-03d3d64790298ccfbcc165ae4ddd92447eac5a5a.tar.bz2
perlweeklychallenge-club-03d3d64790298ccfbcc165ae4ddd92447eac5a5a.zip
Add Perl solution to challenge 277
-rw-r--r--challenge-277/paulo-custodio/Makefile2
-rw-r--r--challenge-277/paulo-custodio/perl/ch-1.pl43
-rw-r--r--challenge-277/paulo-custodio/perl/ch-2.pl40
-rw-r--r--challenge-277/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-277/paulo-custodio/t/test-2.yaml10
5 files changed, 110 insertions, 0 deletions
diff --git a/challenge-277/paulo-custodio/Makefile b/challenge-277/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-277/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-277/paulo-custodio/perl/ch-1.pl b/challenge-277/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..d2bc267287
--- /dev/null
+++ b/challenge-277/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+# Challenge 277
+#
+# Task 1: Count Common
+# Submitted by: Mohammad Sajid Anwar
+# You are given two array of strings, @words1 and @words2.
+#
+# Write a script to return the count of words that appears in both arrays exactly once.
+#
+# Example 1
+# Input: @words1 = ("Perl", "is", "my", "friend")
+# @words2 = ("Perl", "and", "Raku", "are", "friend")
+# Output: 2
+#
+# The words "Perl" and "friend" appear once in each array.
+# Example 2
+# Input: @words1 = ("Perl", "and", "Python", "are", "very", "similar")
+# @words2 = ("Python", "is", "top", "in", "guest", "languages")
+# Output: 1
+# Example 3
+# Input: @words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional")
+# @words2 = ("Crystal", "is", "similar", "to", "Ruby")
+# Output: 0
+
+use Modern::Perl;
+
+my $words = "@ARGV";
+my @words = split /,/, $words;
+
+my $count = [];
+for my $i (0..1) {
+ for my $word (split ' ', $words[$i]) {
+ $count->[$i]{$word}++;
+ }
+}
+
+my $count_same = 0;
+for my $word (split ' ', @words[0..1]) {
+ $count_same++ if ($count->[0]{$word}//0)==1 && ($count->[1]{$word}//0)==1;
+}
+
+say $count_same;
diff --git a/challenge-277/paulo-custodio/perl/ch-2.pl b/challenge-277/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..da33b27d94
--- /dev/null
+++ b/challenge-277/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+# Challenge 277
+#
+# Task 2: Strong Pair
+# Submitted by: Mohammad Sajid Anwar
+# You are given an array of integers, @ints.
+#
+# Write a script to return the count of all strong pairs in the given array.
+#
+# A pair of integers x and y is called strong pair if it satisfies: 0 < |x - y| < min(x, y).
+#
+# Example 1
+# Input: @ints = (1, 2, 3, 4, 5)
+# Ouput: 4
+#
+# Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5)
+# Example 2
+# Input: @ints = (5, 7, 1, 7)
+# Ouput: 1
+#
+# Strong Pairs: (5, 7)
+
+use Modern::Perl;
+use List::Util 'min', 'uniq';
+
+my @ints = uniq @ARGV;
+my $count = 0;
+for my $i (0..$#ints-1) {
+ for my $j ($i+1..$#ints) {
+ $count++ if is_strong_pair($ints[$i], $ints[$j]);
+ }
+}
+
+say $count;
+
+sub is_strong_pair {
+ my($a, $b) = @_;
+ return 0 < abs($a-$b) && abs($a-$b) < min($a,$b);
+}
diff --git a/challenge-277/paulo-custodio/t/test-1.yaml b/challenge-277/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..03040ce356
--- /dev/null
+++ b/challenge-277/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: Perl is my friend,Perl and Raku are friend
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: Perl and Python are very similar,Python is top in guest languages
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: Perl is imperative Lisp is functional,Crystal is similar to Ruby
+ input:
+ output: 0
diff --git a/challenge-277/paulo-custodio/t/test-2.yaml b/challenge-277/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..6b7d7a47dd
--- /dev/null
+++ b/challenge-277/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 1 2 3 4 5
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 5 7 1 7
+ input:
+ output: 1