aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-08-18 15:53:03 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-08-18 15:53:03 +0100
commitffcaa428637c38e3716817bf61e9c9c4c7c97cd8 (patch)
treea13621e04ad6bd42408f0d5adb303b8804db2c59
parentb3d6c27e1de42ddfb8ab2df69083a4cea12f8ca8 (diff)
downloadperlweeklychallenge-club-ffcaa428637c38e3716817bf61e9c9c4c7c97cd8.tar.gz
perlweeklychallenge-club-ffcaa428637c38e3716817bf61e9c9c4c7c97cd8.tar.bz2
perlweeklychallenge-club-ffcaa428637c38e3716817bf61e9c9c4c7c97cd8.zip
Add Perl solution to challenge 225
-rw-r--r--challenge-225/paulo-custodio/Makefile2
-rw-r--r--challenge-225/paulo-custodio/perl/ch-1.pl31
-rw-r--r--challenge-225/paulo-custodio/perl/ch-2.pl76
-rw-r--r--challenge-225/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-225/paulo-custodio/t/test-2.yaml15
5 files changed, 134 insertions, 0 deletions
diff --git a/challenge-225/paulo-custodio/Makefile b/challenge-225/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-225/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-225/paulo-custodio/perl/ch-1.pl b/challenge-225/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..c2ea1a9e1b
--- /dev/null
+++ b/challenge-225/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+# Challenge 225
+#
+# Task 1: Max Words
+# Submitted by: Mohammad S Anwar
+# You are given a list of sentences, @list.
+#
+#
+# A sentence is a list of words that are separated by a single space with no
+# leading or trailing spaces.
+#
+#
+# Write a script to find out the maximum number of words that appear in a
+# single sentence.
+#
+# Example 1
+# Input: @list = ("Perl and Raku belong to the same family.",
+# "I love Perl.",
+# "The Perl and Raku Conference.")
+# Output: 8
+# Example 2
+# Input: @list = ("The Weekly Challenge.",
+# "Python is the most popular guest language.",
+# "Team PWC has over 300 members.")
+# Output: 7
+
+use Modern::Perl;
+use List::Util 'max';
+
+say max(map {scalar(split ' ', $_)} @ARGV);
diff --git a/challenge-225/paulo-custodio/perl/ch-2.pl b/challenge-225/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..2da519c1fb
--- /dev/null
+++ b/challenge-225/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+
+# Challenge 225
+#
+# Task 2: Left Right Sum Diff
+# Submitted by: Mohammad S Anwar
+# You are given an array of integers, @ints.
+#
+# Write a script to return left right sum diff array as shown below:
+#
+#
+# @ints = (a, b, c, d, e)
+#
+# @left = (0, a, (a+b), (a+b+c))
+# @right = ((c+d+e), (d+e), e, 0)
+# @left_right_sum_diff = ( | 0 - (c+d+e) |,
+# | a - (d+e) |,
+# | (a+b) - e |,
+# | (a+b+c) - 0 | )
+#
+# Example 1:
+# Input: @ints = (10, 4, 8, 3)
+# Output: (15, 1, 11, 22)
+#
+# @left = (0, 10, 14, 22)
+# @right = (15, 11, 3, 0)
+#
+# @left_right_sum_diff = ( |0-15|, |10-11|, |14-3|, |22-0|)
+# = (15, 1, 11, 22)
+# Example 2:
+# Input: @ints = (1)
+# Output: (0)
+#
+# @left = (0)
+# @right = (0)
+#
+# @left_right_sum_diff = ( |0-0| ) = (0)
+# Example 3:
+# Input: @ints = (1, 2, 3, 4, 5)
+# Output: (14, 11, 6, 1, 10)
+#
+# @left = (0, 1, 3, 6, 10)
+# @right = (14, 12, 9, 5, 0)
+#
+# @left_right_sum_diff = ( |0-14|, |1-12|, |3-9|, |6-5|, |10-0|)
+# = (14, 11, 6, 1, 10)
+
+use Modern::Perl;
+
+my @ints = @ARGV;
+my @sum = right_sum_diff(@ints);
+say "@sum";
+
+sub right_sum_diff {
+ my(@ints) = @_;
+
+ my $sum = 0;
+ my @left = ($sum);
+ for (@ints[0..$#ints-1]) {
+ $sum += $_;
+ push @left, $sum;
+ }
+
+ $sum = 0;
+ my @right = ($sum);
+ for (reverse @ints[1..$#ints]) {
+ $sum += $_;
+ unshift @right, $sum;
+ }
+
+ my @sum;
+ for (0..$#ints) {
+ push @sum, abs($left[$_]-$right[$_]);
+ }
+ return @sum;
+}
diff --git a/challenge-225/paulo-custodio/t/test-1.yaml b/challenge-225/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..7e9cfd03b2
--- /dev/null
+++ b/challenge-225/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: '"Perl and Raku belong to the same family." "I love Perl." "The Perl and Raku Conference."'
+ input:
+ output: 8
+- setup:
+ cleanup:
+ args: '"The Weekly Challenge." "Python is the most popular guest language." "Team PWC has over 300 members."'
+ input:
+ output: 7
diff --git a/challenge-225/paulo-custodio/t/test-2.yaml b/challenge-225/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..970b197902
--- /dev/null
+++ b/challenge-225/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 10 4 8 3
+ input:
+ output: 15 1 11 22
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 1 2 3 4 5
+ input:
+ output: 14 11 6 1 10