From 3003b5233c05ea3ac06289c09fed1f91c1abde7b Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 22 Jan 2024 08:34:18 +0000 Subject: w253 - Task 1 & 2 --- challenge-253/perlboy1967/perl/ch1.pl | 35 ++++++++++++++++++++++++ challenge-253/perlboy1967/perl/ch2.pl | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 challenge-253/perlboy1967/perl/ch1.pl create mode 100755 challenge-253/perlboy1967/perl/ch2.pl 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; -- cgit