aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-07-14 05:33:38 +0100
committerGitHub <noreply@github.com>2023-07-14 05:33:38 +0100
commitf4d2ab78a5ea8cb69132cfee6c46fce3b98d2a10 (patch)
treeab42430095d3bf3e221358f2f573e871cf485091
parentd47fd7a1320d71d12c211adf0d99d0f8a5bdf41d (diff)
parent293840118563172d0a35e73292e9065135dab0c4 (diff)
downloadperlweeklychallenge-club-f4d2ab78a5ea8cb69132cfee6c46fce3b98d2a10.tar.gz
perlweeklychallenge-club-f4d2ab78a5ea8cb69132cfee6c46fce3b98d2a10.tar.bz2
perlweeklychallenge-club-f4d2ab78a5ea8cb69132cfee6c46fce3b98d2a10.zip
Merge pull request #8370 from pokgopun/pwc-225
pwc225 solution in perl
-rw-r--r--challenge-225/pokgopun/perl/ch-1.pl48
-rw-r--r--challenge-225/pokgopun/perl/ch-2.pl67
2 files changed, 115 insertions, 0 deletions
diff --git a/challenge-225/pokgopun/perl/ch-1.pl b/challenge-225/pokgopun/perl/ch-1.pl
new file mode 100644
index 0000000000..65f32cadb8
--- /dev/null
+++ b/challenge-225/pokgopun/perl/ch-1.pl
@@ -0,0 +1,48 @@
+### 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 strict;
+use warnings;
+
+my @list = @ARGV ? @ARGV :
+
+("Perl and Raku belong to the same family.",
+"I love Perl.",
+"The Perl and Raku Conference."
+);
+#("The Weekly Challenge.",
+#"Python is the most popular guest language.",
+#"Team PWC has over 300 members."
+#);
+
+my @sort = sort map{ scalar(split /\s/, $_) } @list;
+## use Data::Dumper;
+## print Dumper \@sort;
+## exit(0);
+
+printf(
+ "Input: \@list = (%s)\nOutput: %d\n",
+ join( ",\n"." "x16, map{ '"'.$_.'"' } @list ),
+ $sort[-1]
+);
+
diff --git a/challenge-225/pokgopun/perl/ch-2.pl b/challenge-225/pokgopun/perl/ch-2.pl
new file mode 100644
index 0000000000..5ecd8f2d87
--- /dev/null
+++ b/challenge-225/pokgopun/perl/ch-2.pl
@@ -0,0 +1,67 @@
+### 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 strict;
+use warnings;
+
+my @list = @ARGV ? @ARGV :
+(10, 4, 8, 3);
+# (1);
+# (1, 2, 3, 4, 5);
+my $n = @list;
+my @left;
+my @right;
+my ($i, $j, $k) = (0, 0, 0);
+{
+ push @left, $i;
+ $i += $list[$k];
+ unshift @right, $j;
+ $k++;
+ $j += $list[-$k];
+ redo if @left < $n;
+}
+my @diff;
+push(@diff, abs($left[$_] - $right[$_])) foreach 0..$k-1;
+printf "Input: \@ints = (%s)\nOutput: (%s)\n", join(", ", @list), join(", ", @diff);