diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-01-22 15:43:11 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-01-22 15:43:11 -0500 |
| commit | cd3bac6670c12941736df8c90bb799bfbe85dad0 (patch) | |
| tree | bfbed153c21e1e290b3b84edf5333c4def7e0348 | |
| parent | e214c9947ce77a42cf446c9abad0deaff401266e (diff) | |
| download | perlweeklychallenge-club-cd3bac6670c12941736df8c90bb799bfbe85dad0.tar.gz perlweeklychallenge-club-cd3bac6670c12941736df8c90bb799bfbe85dad0.tar.bz2 perlweeklychallenge-club-cd3bac6670c12941736df8c90bb799bfbe85dad0.zip | |
DAJ #253
| -rw-r--r-- | challenge-253/dave-jacoby/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-253/dave-jacoby/perl/ch-2.pl | 61 |
2 files changed, 109 insertions, 0 deletions
diff --git a/challenge-253/dave-jacoby/perl/ch-1.pl b/challenge-253/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..02d8c81875 --- /dev/null +++ b/challenge-253/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + { + words => [ "one.two.three", "four.five", "six" ], + separator => ".", + }, + { + words => [ '$perl$$', '$$raku$' ], + separator => '$', + }, + { + words => [qw{abracadabra}], + separator => 'a', + }, + { + words => [qw{Splits the string EXPR into a list of strings}], + separator => 'i', + } +); + +for my $example (@examples) { + my $input = join ', ', map { qq{"$_"} } $example->{words}->@*; + my @output = split_strings($example); + my $separator = $example->{separator}; + my $output = join ', ', map { qq{"$_"} } @output; + + say <<~"END"; + Input: \@words = ($input) + \$separator = "$separator" + Output: $output + + END +} + +sub split_strings ($hash) { + my $sep = quotemeta( $hash->{separator} ); + my @words = $hash->{words}->@*; + my @output = grep { length $_ } map { split /$sep/, $_ } @words; + return @output; +} diff --git a/challenge-253/dave-jacoby/perl/ch-2.pl b/challenge-253/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..0dd5e50090 --- /dev/null +++ b/challenge-253/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ max sum0 }; + +my @examples = ( + + [ + [ 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 ] + ], +); + +for my $input (@examples) { + my @o = weakest_row($input); + my $output = join ', ', @o; + my $matrix = format_matrix($input); + + say <<~"END"; + Input: \$matrix = $matrix; + Output: ($output) + END +} + +sub weakest_row ($matrix) { + my %rank; + for my $i ( 0 .. -1 + scalar $matrix->@* ) { + my $r = $matrix->[$i]; + my $s = sum0 $r->@*; + $rank{$i} = $s; + } + my @output = sort { $rank{$a} <=> $rank{$b} } sort keys %rank; + return @output; +} + +sub format_matrix ($matrix) { + my $maxlen = max map { length $_ } map { $_->@* } $matrix->@*; + my $output = join "\n ", '[', ( + map { qq{ [$_],} } map { + join ',', + map { pad( $_, 1 + $maxlen ) } + $_->@* + } map { $matrix->[$_] } 0 .. -1 + scalar $matrix->@* + ), + ']'; + return $output; +} + +sub pad ( $str, $len = 4 ) { return sprintf "%${len}s", $str; } |
