From 8db85cd488ebedbbda40cca5403676ce0ed8e072 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 22 Jan 2024 18:45:40 +0000 Subject: - Added solutions by Eric Cheung. - Added solutions by Laurent Rosenfeld. - Added solutions by Mark Anderson. - Added solutions by Niels van Dijke. - Added solutions by PokGoPun. - Added solutions by Luca Ferrari. - Added solutions by David Ferrone. - Added solutions by W. Luis Mochan. --- challenge-253/eric-cheung/python/ch-1.py | 14 ++++++++ challenge-253/eric-cheung/python/ch-2.py | 17 +++++++++ challenge-253/laurent-rosenfeld/blog.txt | 1 + challenge-253/laurent-rosenfeld/perl/ch-1.pl | 18 ++++++++++ challenge-253/laurent-rosenfeld/raku/ch-1.raku | 18 ++++++++++ challenge-253/perlboy1967/perl/ch-1.pl | 35 ++++++++++++++++++ challenge-253/perlboy1967/perl/ch-2.pl | 50 ++++++++++++++++++++++++++ challenge-253/perlboy1967/perl/ch1.pl | 35 ------------------ challenge-253/perlboy1967/perl/ch2.pl | 50 -------------------------- 9 files changed, 153 insertions(+), 85 deletions(-) create mode 100755 challenge-253/eric-cheung/python/ch-1.py create mode 100755 challenge-253/eric-cheung/python/ch-2.py create mode 100644 challenge-253/laurent-rosenfeld/blog.txt create mode 100644 challenge-253/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-253/laurent-rosenfeld/raku/ch-1.raku create mode 100755 challenge-253/perlboy1967/perl/ch-1.pl create mode 100755 challenge-253/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-253/perlboy1967/perl/ch1.pl delete mode 100755 challenge-253/perlboy1967/perl/ch2.pl (limited to 'challenge-253') diff --git a/challenge-253/eric-cheung/python/ch-1.py b/challenge-253/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..ac1c18f9c5 --- /dev/null +++ b/challenge-253/eric-cheung/python/ch-1.py @@ -0,0 +1,14 @@ + +## Example 1 +arrWords = ["one.two.three", "four.five", "six"] +strSeparator = "." + +## Example 2 +## arrWords = ["$perl$$", "$$raku$"] +## strSeparator = "$" + +arrOutput = [] +for strLoop in arrWords: + arrOutput = arrOutput + strLoop.split(strSeparator) + +print (",".join(["\"" + strLoop + "\"" for strLoop in arrOutput if strLoop])) diff --git a/challenge-253/eric-cheung/python/ch-2.py b/challenge-253/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..91e11aaa8b --- /dev/null +++ b/challenge-253/eric-cheung/python/ch-2.py @@ -0,0 +1,17 @@ + +def IsWeaker (rowA, rowB): + return True if rowA.count(1) <= rowB.count(1) else False + +## arrMatrix = [[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]] ## Example 1 +arrMatrix = [[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]] ## Example 2 + +arrIndx = [nIndx for nIndx in range(len(arrMatrix))] + +for nRowLoop in range(len(arrIndx) - 1): + for nColLoop in range(nRowLoop + 1, len(arrIndx)): + if not IsWeaker (arrMatrix[arrIndx[nRowLoop]], arrMatrix[arrIndx[nColLoop]]): + vTemp = arrIndx[nRowLoop] + arrIndx[nRowLoop] = arrIndx[nColLoop] + arrIndx[nColLoop] = vTemp + +print (arrIndx) diff --git a/challenge-253/laurent-rosenfeld/blog.txt b/challenge-253/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..3ce6bf5981 --- /dev/null +++ b/challenge-253/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/01/perl-weekly-challenge-253-split-strings.html diff --git a/challenge-253/laurent-rosenfeld/perl/ch-1.pl b/challenge-253/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..8cccb2a672 --- /dev/null +++ b/challenge-253/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'say'; + +sub split_strings { + my ($sep, @strings) = @_; + $sep = quotemeta $sep; + my @result = grep { /\w+/ } + map { split $sep, $_ } @strings; + return @result; +} + +my @tests = ( [ '.', ["one.two.three","four.five","six"] ], + [ '$', ['$perl$$', '$$raku$'] ] ); +for my $test (@tests) { + printf "%-30s => ", "@{$test->[1]}"; + say join " ", split_strings $test->[0], @{$test->[1]}; +} diff --git a/challenge-253/laurent-rosenfeld/raku/ch-1.raku b/challenge-253/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..48e90b38e6 --- /dev/null +++ b/challenge-253/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,18 @@ +sub split-strings (:$sep, :@strings) { + my @result = grep { /\w+/ }, flat + map { split $sep, $_ }, @strings; + return @result; +} + + +my @tests = { + 'separator' => '.', + 'string' => ("one.two.three","four.five","six") + }, { + 'separator' => '$', + 'string' => ('$perl$$', '$$raku$')}; +for @tests -> %test { + printf "%-30s => ", %test; + say split-strings(sep => %test, + strings => %test); +} diff --git a/challenge-253/perlboy1967/perl/ch-1.pl b/challenge-253/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..b93afab37f --- /dev/null +++ b/challenge-253/perlboy1967/perl/ch-1.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/ch-2.pl b/challenge-253/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..58d1e27c8a --- /dev/null +++ b/challenge-253/perlboy1967/perl/ch-2.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; diff --git a/challenge-253/perlboy1967/perl/ch1.pl b/challenge-253/perlboy1967/perl/ch1.pl deleted file mode 100755 index b93afab37f..0000000000 --- a/challenge-253/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,35 +0,0 @@ -#!/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 deleted file mode 100755 index 58d1e27c8a..0000000000 --- a/challenge-253/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,50 +0,0 @@ -#!/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