diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-08 18:57:37 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-08 18:57:37 +0000 |
| commit | 066a38cc3004d179463b4c3cd5feb18b042ad36f (patch) | |
| tree | 909c2abbfb96bcfa8308e4598252de58ccaf6732 | |
| parent | 4efff4c8f9747d29b66a86556aeef22f18172e1d (diff) | |
| parent | 6e2b8db97d0345746b977c647e11ce48ba88ed54 (diff) | |
| download | perlweeklychallenge-club-066a38cc3004d179463b4c3cd5feb18b042ad36f.tar.gz perlweeklychallenge-club-066a38cc3004d179463b4c3cd5feb18b042ad36f.tar.bz2 perlweeklychallenge-club-066a38cc3004d179463b4c3cd5feb18b042ad36f.zip | |
Merge pull request #9376 from pme/challenge-251
challenge-251
| -rwxr-xr-x | challenge-251/peter-meszaros/perl/ch-1.pl | 83 | ||||
| -rwxr-xr-x | challenge-251/peter-meszaros/perl/ch-2.pl | 83 |
2 files changed, 166 insertions, 0 deletions
diff --git a/challenge-251/peter-meszaros/perl/ch-1.pl b/challenge-251/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..b8f84a04f2 --- /dev/null +++ b/challenge-251/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl +# +# 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. +# +# For example, the concatenation of 10, 21 is 1021. +# The concatenation value of @ints is initially equal to 0. +# Perform this operation until @ints becomes empty: +# +# If there exists more than one number in @ints, pick the first element +# and last element in @ints respectively and add the value of their +# concatenation to the concatenation value of @ints, then delete the +# first and last element from @ints. +# +# If one element exists, add its value to the concatenation value of +# @ints, then delete it. +# +# Example 1 +# +# Input: @ints = (6, 12, 25, 1) +# Output: 1286 +# +# 1st operation: concatenation of 6 and 1 is 61 +# 2nd operation: concaternation of 12 and 25 is 1225 +# +# Concatenation Value => 61 + 1225 => 1286 +# +# Example 2 +# +# Input: @ints = (10, 7, 31, 5, 2, 2) +# Output: 489 +# +# 1st operation: concatenation of 10 and 2 is 102 +# 2nd operation: concatenation of 7 and 2 is 72 +# 3rd operation: concatenation of 31 and 5 is 315 +# +# Concatenation Value => 102 + 72 + 315 => 489 +# +# Example 3 +# +# Input: @ints = (1, 2, 10) +# Output: 112 +# +# 1st operation: concatenation of 1 and 10 is 110 +# 2nd operation: only element left is 2 +# +# Concatenation Value => 110 + 2 => 112 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [6, 12, 25, 1], + [10, 7, 31, 5, 2, 2], + [1, 2, 10], +]; + +sub concatenation_value +{ + my $l = shift; + + my $v = 0; + for my $i (0..($#$l/2)) { + my $j = $#$l - $i; + $v += ($i == $j) ? $l->[$i] : ($l->[$i] . $l->[$j]); + } + + return $v; +} + +is(concatenation_value($cases->[0]), 1286, 'Example 1'); +is(concatenation_value($cases->[1]), 489, 'Example 2'); +is(concatenation_value($cases->[2]), 112, 'Example 3'); +done_testing(); + +exit 0; diff --git a/challenge-251/peter-meszaros/perl/ch-2.pl b/challenge-251/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..0c58675271 --- /dev/null +++ b/challenge-251/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl +# +# 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. +# +# Example 1 +# +# Input: $matrix = [ [ 3, 7, 8], +# [ 9, 11, 13], +# [15, 16, 17] ]; +# Output: 15 +# +# 15 is the only lucky number since it is the minimum in its row +# and the maximum in its column. +# +# Example 2 +# +# Input: $matrix = [ [ 1, 10, 4, 2], +# [ 9, 3, 8, 7], +# [15, 16, 17, 12] ]; +# Output: 12 +# +# Example 3 +# +# Input: $matrix = [ [7 ,8], +# [1 ,2] ]; +# Output: 7 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::Util qw/min/; + +my $cases = [ + [[ 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]], +]; + +sub lucky_numbers +{ + my $l = shift; + + my (@rowmin, @colmax); + for my $row (@$l) { + push @rowmin, min $row->@*; + for my $i (0..$#$row) { + $colmax[$i] = $row->[$i] if ($colmax[$i] // 0) < $row->[$i]; + } + } + + my $lucky = -1; + my %hash = map {$_ => 1} @rowmin; + for my $v (@colmax) { + if (exists $hash{$v}) { + $lucky = $v; + last; + } + $hash{$v}++; + } + + return $lucky; +} + +is(lucky_numbers($cases->[0]), 15, 'Example 1'); +is(lucky_numbers($cases->[1]), 12, 'Example 2'); +is(lucky_numbers($cases->[2]), 7, 'Example 3'); +done_testing(); + +exit 0; + + |
