diff options
| -rw-r--r-- | challenge-253/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-253/bob-lied/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-253/bob-lied/perl/ch-2.pl | 73 |
3 files changed, 126 insertions, 3 deletions
diff --git a/challenge-253/bob-lied/README b/challenge-253/bob-lied/README index 8b958edfe8..af6f9a022d 100644 --- a/challenge-253/bob-lied/README +++ b/challenge-253/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 252 by Bob Lied +Solutions to weekly challenge 253 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-252/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-252/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-253/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-253/bob-lied diff --git a/challenge-253/bob-lied/perl/ch-1.pl b/challenge-253/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..6144e22dbf --- /dev/null +++ b/challenge-253/bob-lied/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 253 Task 1 Split Strings +#============================================================================= +# 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. +# Example 1 Input: @words = ("one.two.three","four.five","six") $separator = "." +# Output: "one","two","three","four","five","six" +# Example 2 Input: @words = ("$perl$$", "$$raku$") $separator = "$" +# Output: "perl","raku" +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; +my $Separator = " "; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "sep:s" => \$Separator); +exit(!runTest()) if $DoTest; + +say join ",", map { qq("$_") } splitStrings(\@ARGV, $Separator)->@*; + +sub splitStrings($words, $separator) +{ + my @output = grep !/\A\Z/, map { split /\Q$separator\E+/, $_ } $words->@*; + return \@output; +} + +sub runTest +{ + use Test2::V0; + + is( splitStrings( ["one.two.three", "four.five", "six" ], "." ), + [ qw(one two three four five six) ], "Example 1"); + is( splitStrings( [ '$perl$$', '$$raku$' ], '$' ), + [ qw( perl raku ) ], "Example 1"); + + is( splitStrings( [ qw(a b c) ], '/' ), [ qw(a b c) ], "No separator"); + is( splitStrings( [ "////", "//", "/", ], "/" ), [ ], "No words"); + + done_testing; +} diff --git a/challenge-253/bob-lied/perl/ch-2.pl b/challenge-253/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..23151598e2 --- /dev/null +++ b/challenge-253/bob-lied/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 253 Task 2 Weakest Row +#============================================================================= +# 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. +# Example 1 Input: $matrix = [ [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] ] +# Output: (2, 0, 3, 1, 4) +# Example 2 Input: $matrix = [ [1, 0, 0, 0], +# [1, 1, 1, 1], +# [1, 0, 0, 0], +# [1, 0, 0, 0] ] +# Output: (0, 2, 3, 1) +#============================================================================= + +use v5.38; + +use builtin qw/trim/; no warnings "experimental::builtin"; +use List::Util qw/sum/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +# USAGE: give rows as strings on command line, e.g. +# ./ch-1.pl "[1,0,0]" "[0,1,0]" "[0,0,1]" +# ./ch-1.pl 1,0,0 0,1,0 0,0,1 +my @r = map { trim($_) } map { (s/[^01]+/ /gr) } @ARGV; +say join "|", @r if $Verbose; +my @matrix = map { [ split ' ', $_ ] } @r; +say "(", join(", ", weakestRow(\@matrix)->@*), ")"; + +sub weakestRow($m) +{ + my @strength = map { sum $_->@* } $m->@*; + + return [ sort { $strength[$a] <=> $strength[$b] || $a <=> $b } 0 .. $m->$#* ]; +} + +sub runTest +{ + use Test2::V0; + + my $matrix; + $matrix = [ [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] ]; + is(weakestRow($matrix), [ 2,0,3,1,4 ], "Example 1"); + + $matrix = [ [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] ]; + is(weakestRow($matrix), [ 0,2,3,1 ], "Example 2"); + + done_testing; +} |
