The Weekly Challenge - 251 (January 8th 2024)
Task #1: Concatenation Value
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.
Example 1
For example, the concatenation of 10, 21 is 1021.
Example 2
Input: @ints = (6, 12, 25, 1) Output: 1286
“6”.”1″ + “12”.”25″
Example 3
Input: @ints = (1, 2, 10) Output: 112
“1”.”10″ + “2”
Solution
This is straight forward we take first and last concatenate and add to a running total:
Notes:
pop
andshift
without a parameter work on@_
so we don’t need to specify it.- In the odd length array the
shift.pop
works asshift
returns the value in the list andpop
an empty string.
251-1-concatenation-value.pl - perl
sub concatenation_value {
my $t = 0;
$t += shift.pop while @_;
$t
}
Task #2: Lucky Numbers
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.
Example 1
[ [ 3, 7, 8], [ 9, 11, 13], [15, 16, 17] ]; Output: 15
First column, last row
Example 2
[ [ 1, 10, 4, 2], [ 9, 3, 8, 7], [15, 16, 17, 12] ]; Output: 12
Example 3
[ [7 ,8], [1 ,2] ]; Output: 7
Example 4
[ [ 4, 2 ] [ 2, 4 ] ] Output: -1
No value matches criteria
Solution
This assumes nothing {can simplify a bit if you make assumptions about solutions} but this is still O(n.m)
251-2-lucky-number.pl - perl
sub lucky_number {
my(@m,@l) = map {
my $min = $_->[0];
( $min>$_ ) && ( $min=$_ ) for @{$_};
[ map { ($_ == $min) ? 1 : 0 } @{$_} ]
} my @d = map { [@{$_}] } @_;
while(@{$d[0]}){
my @t = map { shift @{$_} } @m;
my @c = map { shift @{$_} } @d;
my $max = $c[0];
( $max<$_ ) && ( $max=$_ ) for @c;
push @l, grep { shift @t && $_==$max } @c;
}
@l ? @l : -1
}
About The Weekly Challenge
The weekly challenge was set up Mohammad Anwar approximately 5 years ago - each Monday two problems are set for the "team" to solve.
These are posted at: