diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-08 18:57:00 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-08 18:57:00 +0000 |
| commit | 4efff4c8f9747d29b66a86556aeef22f18172e1d (patch) | |
| tree | ec25506083502a628f69dfa94de0b8cf83947227 | |
| parent | 427e60c67b782623123845b80474a8cb4da0ff2f (diff) | |
| parent | bac8160bd6d93442b20aef4e8b5d396939b55c42 (diff) | |
| download | perlweeklychallenge-club-4efff4c8f9747d29b66a86556aeef22f18172e1d.tar.gz perlweeklychallenge-club-4efff4c8f9747d29b66a86556aeef22f18172e1d.tar.bz2 perlweeklychallenge-club-4efff4c8f9747d29b66a86556aeef22f18172e1d.zip | |
Merge pull request #9375 from jacoby/master
DAJ #251
| -rw-r--r-- | challenge-251/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-251/dave-jacoby/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-251/dave-jacoby/perl/ch-2.pl | 63 |
3 files changed, 102 insertions, 0 deletions
diff --git a/challenge-251/dave-jacoby/blog.txt b/challenge-251/dave-jacoby/blog.txt new file mode 100644 index 0000000000..61c31e441a --- /dev/null +++ b/challenge-251/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2024/01/08/do-you-feel-lucky-weekly-challenge-251.html diff --git a/challenge-251/dave-jacoby/perl/ch-1.pl b/challenge-251/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..3d144f59f4 --- /dev/null +++ b/challenge-251/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + [ 6, 12, 25, 1 ], + [ 10, 7, 31, 5, 2, 2 ], + [ 1, 2, 10 ], +); + +for my $example (@examples) { + my $input = join ', ', $example->@*; + my $output = kinda_concatenation( $example->@* ); + + say <<~"END"; + Input: \$ints = ($input) + Output: $output + END +} + +sub kinda_concatenation (@input) { + my @output; + while (@input) { + if ( scalar @input == 1 ) { + push @output, shift @input; + } + else { + my $concat = join '', shift @input, pop @input; + push @output, $concat; + } + } + return sum0 @output; +} diff --git a/challenge-251/dave-jacoby/perl/ch-2.pl b/challenge-251/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..d864ab450f --- /dev/null +++ b/challenge-251/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ min max }; +use JSON; +my $j = JSON->new->pretty; + +my @examples = ( + + [ + [ 3, 7, 8 ], + [ 9, 11, 13 ], + [ 15, 16, 17 ] + ], + [ + [ 1, 10, 4, 2 ], + [ 9, 3, 8, 7 ], + [ 15, 16, 17, 12 ] + ], + [ + [ 7, 8 ], + [ 1, 2 ] + ], +); +for my $e (@examples) { + my $input = format_matrix($e); + my $output = lucky_numbers($e); + + say <<~"END"; + Input: \$matrix = $input + Output: $output + END +} + +sub lucky_numbers ($matrix) { + for my $i ( 0 .. -1 + scalar $matrix->@* ) { + for my $j ( 0 .. -1 + scalar $matrix->[$i]->@* ) { + my $value = $matrix->[$i][$j]; + my @row = $matrix->[$i]->@*; + my @col = map { $matrix->[$_][$j] } 0 .. -1 + scalar $matrix->@*; + return $value if ( $value == min @row ) && ( $value == max @col ); + } + } + return -1; #no luck +} + +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; } |
