aboutsummaryrefslogtreecommitdiff
path: root/challenge-253
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2024-01-22 08:34:18 +0000
committerNiels van Dijke <perlboy@cpan.org>2024-01-22 08:34:18 +0000
commit3003b5233c05ea3ac06289c09fed1f91c1abde7b (patch)
tree6f768d1907144dd854969bacdf1eb0231fe535f1 /challenge-253
parent9d7dc816f7775abfee7d90f0a2a969611902be54 (diff)
downloadperlweeklychallenge-club-3003b5233c05ea3ac06289c09fed1f91c1abde7b.tar.gz
perlweeklychallenge-club-3003b5233c05ea3ac06289c09fed1f91c1abde7b.tar.bz2
perlweeklychallenge-club-3003b5233c05ea3ac06289c09fed1f91c1abde7b.zip
w253 - Task 1 & 2
Diffstat (limited to 'challenge-253')
-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;