aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-22 18:15:26 +0000
committerGitHub <noreply@github.com>2024-01-22 18:15:26 +0000
commit7517c26ea8016efb870321f69075991e6c3b8d8c (patch)
treeb2322cd77300346596bfa20ecb6f0d5322defe67
parent829d8e49258ac52156df32d7d6d93386d5e3b582 (diff)
parent3003b5233c05ea3ac06289c09fed1f91c1abde7b (diff)
downloadperlweeklychallenge-club-7517c26ea8016efb870321f69075991e6c3b8d8c.tar.gz
perlweeklychallenge-club-7517c26ea8016efb870321f69075991e6c3b8d8c.tar.bz2
perlweeklychallenge-club-7517c26ea8016efb870321f69075991e6c3b8d8c.zip
Merge pull request #9442 from PerlBoy1967/branch-for-challenge-253
w253 - Task 1 & 2
-rwxr-xr-xchallenge-253/perlboy1967/perl/ch1.pl35
-rwxr-xr-xchallenge-253/perlboy1967/perl/ch2.pl50
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-253/perlboy1967/perl/ch1.pl b/challenge-253/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..b93afab37f
--- /dev/null
+++ b/challenge-253/perlboy1967/perl/ch1.pl
@@ -0,0 +1,35 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 253
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-253
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Split Strings
+Submitted by: Mohammad S Anwar
+
+You are given an array of strings and a character separator.
+
+Write a script to return all words separated by the given character excluding empty string.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+sub splitStrings ($sep,@str) {
+ $sep = quotemeta $sep;
+ grep /\S/, map { split $sep } @str;
+}
+
+is([splitStrings('.','one.two.three','four.five','six')],
+ ['one','two','three','four','five','six']);
+is([splitStrings('$','$perl$$','$$raku$')],
+ ['perl','raku']);
+
+done_testing;
diff --git a/challenge-253/perlboy1967/perl/ch2.pl b/challenge-253/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..58d1e27c8a
--- /dev/null
+++ b/challenge-253/perlboy1967/perl/ch2.pl
@@ -0,0 +1,50 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 253
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-253
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Weakest Row
+Submitted by: Mohammad S Anwar
+
+You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0.
+
+A row i is weaker than a row j if one of the following is true:
+
+|| a) The number of 1s in row i is less than the number of 1s in row j.
+|| b) Both rows have the same number of 1 and i < j.
+
+Write a script to return the order of rows from weakest to strongest.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+use List::Util qw(sum0);
+
+sub weakestRow ($ar) {
+ my $i = 0;
+ my @s = map { [$i++,sum0 @$_] } @$ar;
+ map { $$_[0] } sort { $$a[1] <=> $$b[1] or $$a[0] <=> $$b[0] } @s;
+}
+
+is([weakestRow([
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 0],
+ [1, 0, 0, 0, 0],
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 1]])],[2,0,3,1,4]);
+is([weakestRow([
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]])], [0,2,3,1]);
+
+done_testing;