aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2024-05-02 21:14:38 +0200
committerpme <hauptadler@gmail.com>2024-05-02 21:14:38 +0200
commitcda82704f8d0fd94aea8653fb8ef73f719a05532 (patch)
tree872fca8c1cd707cd34b3cb707f5b4c50512dfbe8
parent5679e227b2e6bdd10fb6f03f6b622d38f2a8a75b (diff)
downloadperlweeklychallenge-club-cda82704f8d0fd94aea8653fb8ef73f719a05532.tar.gz
perlweeklychallenge-club-cda82704f8d0fd94aea8653fb8ef73f719a05532.tar.bz2
perlweeklychallenge-club-cda82704f8d0fd94aea8653fb8ef73f719a05532.zip
challenge-225
-rwxr-xr-xchallenge-225/peter-meszaros/perl/ch-1.pl58
-rwxr-xr-xchallenge-225/peter-meszaros/perl/ch-2.pl89
2 files changed, 147 insertions, 0 deletions
diff --git a/challenge-225/peter-meszaros/perl/ch-1.pl b/challenge-225/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..8890f74539
--- /dev/null
+++ b/challenge-225/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Max Words
+
+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.
+
+=head2 Example 1
+
+ Input: @list = ("Perl and Raku belong to the same family.",
+ "I love Perl.",
+ "The Perl and Raku Conference.")
+ Output: 8
+
+=head2 Example 2
+
+ Input: @list = ("The Weekly Challenge.",
+ "Python is the most popular guest language.",
+ "Team PWC has over 300 members.")
+ Output: 7
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [["Perl and Raku belong to the same family.",
+ "I love Perl.",
+ "The Perl and Raku Conference."], 8],
+ [["The Weekly Challenge.",
+ "Python is the most popular guest language.",
+ "Team PWC has over 300 members."], 7],
+];
+
+sub max_words
+{
+ my $l = shift;
+
+ my $max_words = 0;
+ for my $m (@$l) {
+ my $n = split / /, $m;
+ $max_words = $n if $n > $max_words;
+ }
+ return $max_words;
+}
+
+for (@$cases) {
+ is(max_words($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-225/peter-meszaros/perl/ch-2.pl b/challenge-225/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..ff3c08efe5
--- /dev/null
+++ b/challenge-225/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,89 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Left Right Sum Diff
+
+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 | )
+
+=head2 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)
+
+=head2 Example 2:
+
+ Input: @ints = (1)
+ Output: (0)
+
+ @left = (0)
+ @right = (0)
+
+ @left_right_sum_diff = ( |0-0| ) = (0)
+
+=head2 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)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use List::Util qw/uniqint/;
+
+my $cases = [
+ [[10, 4, 8, 3], [15, 1, 11, 22]],
+ [[1], [0]],
+ [[1, 2, 3, 4, 5], [14, 11, 6, 1, 10]],
+];
+
+sub left_rigth_sum_diff
+{
+ my $l = shift;
+
+ my (@left, @rght);
+
+ $left[0] = $rght[$#$l] = 0;
+ for my $i (0 .. $#$l) {
+ my $v = $l->[$i];
+ $left[$_] += $v for $i+1 .. $#$l;
+ $rght[$_] += $v for 0 .. $i-1;
+ }
+
+ my @res;
+ $res[$_] = abs($left[$_] - $rght[$_]) for 0 .. $#$l;
+
+ return \@res;
+}
+
+for (@$cases) {
+ is(left_rigth_sum_diff($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
+