From da0d5dcf50f8fa8def91abaafce1fd5624ec1a61 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 8 Jan 2024 19:15:36 +0000 Subject: i- Added solutions by Mark Anderson. - Added solutions by PokGoPun. - Added solutions by Simon Proctor. - Added solutions by Luca Ferrari. - Added solutions by Thomas Kohler. - Added solutions by Peter Campbell Smith. - Added solutions by Niels van Dijke. - Added solutions by W. Luis Mochan. - Added solutions by David Ferrone. - Added solutions by Dave Jacoby. - Added solutions by Peter Meszaros. - Added solutions by Laurent Rosenfeld. --- challenge-251/laurent-rosenfeld/blog.txt | 1 + challenge-251/laurent-rosenfeld/perl/ch-1.pl | 18 ++++++++++ challenge-251/laurent-rosenfeld/raku/ch-1.raku | 13 +++++++ challenge-251/perlboy1967/perl/ch-1.pl | 41 ++++++++++++++++++++++ challenge-251/perlboy1967/perl/ch-2.pl | 48 ++++++++++++++++++++++++++ challenge-251/perlboy1967/perl/ch1.pl | 41 ---------------------- challenge-251/perlboy1967/perl/ch2.pl | 48 -------------------------- 7 files changed, 121 insertions(+), 89 deletions(-) create mode 100644 challenge-251/laurent-rosenfeld/blog.txt create mode 100644 challenge-251/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-251/laurent-rosenfeld/raku/ch-1.raku create mode 100755 challenge-251/perlboy1967/perl/ch-1.pl create mode 100755 challenge-251/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-251/perlboy1967/perl/ch1.pl delete mode 100755 challenge-251/perlboy1967/perl/ch2.pl (limited to 'challenge-251') diff --git a/challenge-251/laurent-rosenfeld/blog.txt b/challenge-251/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..312e6abc5b --- /dev/null +++ b/challenge-251/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/01/perl-weekly-challenge-251-concatenation-value.html diff --git a/challenge-251/laurent-rosenfeld/perl/ch-1.pl b/challenge-251/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..8ff2ab1549 --- /dev/null +++ b/challenge-251/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'say'; + +sub concat_vals { + my @in = @_; + my $concat; + while (@in > 1) { + $concat += (shift @in) . (pop @in); + } + $concat += shift @in if @in > 0; # if we have 1 item left + return $concat; +} + +for my $test ([<6 12 25 1>], [<10 7 31 5 2 2>], [<1 2 10>]) { + printf "%-15s => ", "@$test"; + say concat_vals @$test; +} diff --git a/challenge-251/laurent-rosenfeld/raku/ch-1.raku b/challenge-251/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..341957a396 --- /dev/null +++ b/challenge-251/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,13 @@ +sub concat-vals (@in is copy) { + my $concat; + while @in.elems > 1 { + $concat += @in.shift ~ @in.pop; + } + $concat += shift @in if @in.elems > 0; # last item if any + return $concat; +} + +for <6 12 25 1>, <10 7 31 5 2 2>, <1 2 10> -> @test { + printf "%-15s => ", "@test[]"; + say concat-vals @test; +} diff --git a/challenge-251/perlboy1967/perl/ch-1.pl b/challenge-251/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..57f97135fc --- /dev/null +++ b/challenge-251/perlboy1967/perl/ch-1.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/ch-2.pl b/challenge-251/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..60cd0517ed --- /dev/null +++ b/challenge-251/perlboy1967/perl/ch-2.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; diff --git a/challenge-251/perlboy1967/perl/ch1.pl b/challenge-251/perlboy1967/perl/ch1.pl deleted file mode 100755 index 57f97135fc..0000000000 --- a/challenge-251/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,41 +0,0 @@ -#!/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 deleted file mode 100755 index 60cd0517ed..0000000000 --- a/challenge-251/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,48 +0,0 @@ -#!/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; -- cgit