diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2024-01-08 15:12:17 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2024-01-08 15:12:17 +0000 |
| commit | 9193caa0085c4e69bdec566035803303ca1ad328 (patch) | |
| tree | c2b3b2c668d40c11218069a3195e683cda23a2a7 | |
| parent | 9a485c9bac8e3887b165d67c9aa81d71cdd42f01 (diff) | |
| download | perlweeklychallenge-club-9193caa0085c4e69bdec566035803303ca1ad328.tar.gz perlweeklychallenge-club-9193caa0085c4e69bdec566035803303ca1ad328.tar.bz2 perlweeklychallenge-club-9193caa0085c4e69bdec566035803303ca1ad328.zip | |
w251 - Task 1 & 2
| -rwxr-xr-x | challenge-251/perlboy1967/perl/ch1.pl | 41 | ||||
| -rwxr-xr-x | challenge-251/perlboy1967/perl/ch2.pl | 48 |
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-251/perlboy1967/perl/ch1.pl b/challenge-251/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..57f97135fc --- /dev/null +++ b/challenge-251/perlboy1967/perl/ch1.pl @@ -0,0 +1,41 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 251 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-251 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Concatenation Value +Submitted by: Mohammad S Anwar + +You are given an array of integers, @ints. + +Write a script to find the concatenation value of the given array. + +The concatenation of two numbers is the number formed by concatenating +their numerals. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::Util qw(sum0); + +sub concatenationValue (@ints) { + # splice in an extra 0 if the list has odd number of elements + splice(@ints,$#ints/2-1,1,$ints[$#ints/2-1],0) if ($#ints % 2 == 0); + my $i = 0; + sum0 map { "$ints[$_]$ints[--$i]" } 0 .. $#ints/2; +} + +is(concatenationValue(6,12,25,1),1286); +is(concatenationValue(10,7,31,5,2,2),489); +is(concatenationValue(1,2,10),112); + +done_testing; diff --git a/challenge-251/perlboy1967/perl/ch2.pl b/challenge-251/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..60cd0517ed --- /dev/null +++ b/challenge-251/perlboy1967/perl/ch2.pl @@ -0,0 +1,48 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 251 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-251 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Lucky Numbers +Submitted by: Mohammad S Anwar + +You are given a m x n matrix of distinct numbers. + +Write a script to return the lucky number, if there is one, or -1 if not. + +A lucky number is an element of the matrix such that it is +the minimum element in its row and maximum in its column. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::Util qw(min max); +use List::MoreUtils qw(duplicates); + +sub luckyNumber ($ar) { + my @min = map { min @{$$ar[$_]} } 0 .. $#$ar; + my @max = map { my $c = $_; + max map { $$ar[$_][$c] } 0 .. $#$ar + } 0 .. $#{$ar->[0]}; + (duplicates @min,@max)[0]; +} + +is(luckyNumber([ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]),15); +is(luckyNumber([ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ]),12); +is(luckyNumber([ [7 ,8], + [1 ,2] ]), 7); + +done_testing; |
