Concatenation Value & Lucky Numbers
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.
Starting from 0, repeatedly pick the first and last element, add their concatenation to the total, and remove both elements. If a single element remains, add its value directly and remove it.
Instead of mutating the input array by repeatedly shifting and popping elements, we use a two-pointer scan with indices left = 0 and right = n - 1:
left < right, concatenate the decimal string representations of ints[left] and ints[right], convert to an integer, add to the accumulator, and move both pointers inward.left == right when the loop finishes (odd-length array), add ints[left] directly.This runs in O(n) time and O(1) auxiliary space without modifying the caller's input.
#!/usr/bin/env perl
use v5.38;
use warnings;
use Type::Params -sigs;
use Types::Standard qw(ArrayRef Int);
use Test::More;
signature_for concatenation_value => (
method => 0,
positional => [ ArrayRef [Int] ],
);
sub concatenation_value ($ints) {
my $concat_value = 0;
my $left = 0;
my $right = $#{$ints};
while ( $left < $right ) {
$concat_value += int( "$ints->[$left]$ints->[$right]" );
$left++;
$right--;
}
if ( $left == $right ) {
$concat_value += $ints->[$left];
}
return $concat_value;
}#!/usr/bin/env python3
"""Concatenation Value - Perl Weekly Challenge 251, Task 1."""
from __future__ import annotations
import unittest
def concatenation_value(ints: list[int]) -> int:
"""Return the concatenation value of the given list of integers."""
concat_value = 0
left = 0
right = len(ints) - 1
while left < right:
concat_value += int(f"{ints[left]}{ints[right]}")
left += 1
right -= 1
if left == right:
concat_value += ints[left]
return concat_valueYou are given an 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 the maximum element in its column.
Because all numbers in the matrix are distinct, each row has a unique minimum element. For each row:
min_row and its column index min_idx.min_row is greater than or equal to every element in column min_idx (i.e., it equals the column maximum).min_row immediately; otherwise continue, returning -1 if no row yields a lucky number.Time complexity is O(m · (n + m)) and space complexity is O(1) extra space.
#!/usr/bin/env perl
use v5.38;
use warnings;
use List::Util qw(max min);
use Type::Params -sigs;
use Types::Standard qw(ArrayRef Int);
use Test::More;
signature_for lucky_number => (
method => 0,
positional => [ ArrayRef [ ArrayRef [Int] ] ],
);
sub lucky_number ($matrix) {
for my $row ( @{$matrix} ) {
next if !@{$row};
my $min_row = min( @{$row} );
my ($min_idx) = grep { $row->[$_] == $min_row } 0 .. $#{$row};
my $max_col = max( map { $_->[$min_idx] } @{$matrix} );
return $min_row if $min_row == $max_col;
}
return -1;
}#!/usr/bin/env python3
"""Lucky Numbers - Perl Weekly Challenge 251, Task 2."""
from __future__ import annotations
import unittest
def lucky_number(matrix: list[list[int]]) -> int:
"""Return the lucky number in matrix, or -1 if none exists.
A lucky number is an element that is the minimum in its row
and the maximum in its column.
"""
for row in matrix:
if not row:
continue
min_row = min(row)
min_index = row.index(min_row)
if all(other_row[min_index] <= min_row for other_row in matrix):
return min_row
return -1