diff options
| author | pme <hauptadler@gmail.com> | 2024-01-23 18:32:44 +0100 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-01-23 18:32:44 +0100 |
| commit | 4b6771537e7d6fc1d9f89733c697566d08465181 (patch) | |
| tree | 7dec7c716fa2b2f6915f2003934206b5046e1858 | |
| parent | 85ec742ed6cf1ef0bc460a77a0877bf944b7924f (diff) | |
| download | perlweeklychallenge-club-4b6771537e7d6fc1d9f89733c697566d08465181.tar.gz perlweeklychallenge-club-4b6771537e7d6fc1d9f89733c697566d08465181.tar.bz2 perlweeklychallenge-club-4b6771537e7d6fc1d9f89733c697566d08465181.zip | |
challenge-253
| -rwxr-xr-x | challenge-253/peter-meszaros/perl/ch-1.pl | 51 | ||||
| -rwxr-xr-x | challenge-253/peter-meszaros/perl/ch-2.pl | 90 |
2 files changed, 141 insertions, 0 deletions
diff --git a/challenge-253/peter-meszaros/perl/ch-1.pl b/challenge-253/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..ae0d13f9d1 --- /dev/null +++ b/challenge-253/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +# +# 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 strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [['one.two.three', 'four.five', 'six'], '.'], + [['$perl$$', '$$raku$'], '$'], +]; + +sub split_string +{ + my $list = shift; + my $sep = shift; + + my $res; + my $s = "\\$sep"; + for my $l (@$list) { + $l =~ s/$s/ /g; + push @$res, split(' ', $l); + } + return $res; +} + +is(split_string($cases->[0]->@*), + ['one', 'two', 'three', 'four', 'five', 'six'], + 'Example 1'); +is(split_string($cases->[1]->@*), + ['perl', 'raku'], + 'Example 2'); +done_testing(); + +exit 0; diff --git a/challenge-253/peter-meszaros/perl/ch-2.pl b/challenge-253/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..0dd505fc0f --- /dev/null +++ b/challenge-253/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,90 @@ +#!/usr/bin/env perl +# +# 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) +# +# The number of 1s in each row is: +# - Row 0: 2 +# - Row 1: 4 +# - Row 2: 1 +# - Row 3: 2 +# - Row 4: 5 +# +# 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) +# +# The number of 1s in each row is: +# - Row 0: 1 +# - Row 1: 4 +# - Row 2: 1 +# - Row 3: 1 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::Util qw/sum/; + +my $cases = [ + [ + [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], + ], + [ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0], + ] +]; + +sub weakest_row +{ + my $m = shift; + + my %h; + for my $i (0..$#$m) { + $h{$i} = sum($m->[$i]->@*); + } + + my @res = sort { + $h{$a} == $h{$b} ? $a <=> $b : $h{$a} <=> $h{$b} + } keys %h; + return \@res; +} + +is(weakest_row($cases->[0]), [2, 0, 3, 1, 4], 'Example 1'); +is(weakest_row($cases->[1]), [0, 2, 3, 1], 'Example 2'); +done_testing(); + +exit 0; + + |
