diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-01-16 11:45:23 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-01-16 11:45:23 +0000 |
| commit | 722a465192340594398a547cd26740855a88f805 (patch) | |
| tree | 2e853486643e5e6489de8c523e4acb7b7bee15f4 | |
| parent | 6635e6bd2166b38897dcead55c1c3aa795ea3e0b (diff) | |
| download | perlweeklychallenge-club-722a465192340594398a547cd26740855a88f805.tar.gz perlweeklychallenge-club-722a465192340594398a547cd26740855a88f805.tar.bz2 perlweeklychallenge-club-722a465192340594398a547cd26740855a88f805.zip | |
Add Perl solution
| -rw-r--r-- | challenge-251/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-251/paulo-custodio/perl/ch-1.pl | 73 | ||||
| -rw-r--r-- | challenge-251/paulo-custodio/perl/ch-2.pl | 99 | ||||
| -rw-r--r-- | challenge-251/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-251/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 204 insertions, 0 deletions
diff --git a/challenge-251/paulo-custodio/Makefile b/challenge-251/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-251/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-251/paulo-custodio/perl/ch-1.pl b/challenge-251/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..d61492ca82 --- /dev/null +++ b/challenge-251/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl + +# Challenge 251 +# +# 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. +# +# 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 Modern::Perl; + +my @ints = @ARGV; +say sum_concat(@ints); + +sub sum_concat { + my(@ints) = @_; + my $sum = 0; + while (@ints > 1) { + my $n = $ints[0] . $ints[-1]; + $sum += $n; + pop @ints; shift @ints; + } + $sum += $ints[0] if @ints; + return $sum; +} diff --git a/challenge-251/paulo-custodio/perl/ch-2.pl b/challenge-251/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..7d768473b0 --- /dev/null +++ b/challenge-251/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,99 @@ +#!/usr/bin/env perl + +# Challenge 251 +# +# 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. +# +# +# 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 Modern::Perl; + +my @matrix = parse_matrix("@ARGV"); +say lucky_number(@matrix); + +sub parse_matrix { + my($input) = @_; + my @matrix; + $input =~ s/^\s*\[\s*//s or die "missing starting [ in '$input'"; + $input =~ s/\s*\]\s*$//s or die "missing ending ] in '$input'"; + $input =~ s/^\s+//s; + while ($input ne '') { + ($input, my @row) = parse_row($input); + push @matrix, \@row; + $input =~ s/^\s*,?\s*//s; + } + return @matrix; +} + +sub parse_row { + my($input) = @_; + $input =~ s/^\s*\[\s*//s or die "missing starting [ in '$input'"; + (my $row, $input) = $input =~ /(.*?)\]\s*(.*)/s or die die "missing ending ] in '$input'"; + my @row = split(/(?:\s|,)+/, $row); + return ($input, @row); +} + +sub lucky_number { + my(@matrix) = @_; + for my $r (0 .. $#matrix) { + my $row = $matrix[$r]; + my $c = min_col_in_row(@$row); + my $max_r = max_row_in_col($c, @matrix); + if ($r == $max_r) { + return $matrix[$r][$c]; + } + } + return -1; +} + +sub min_col_in_row { + my(@row) = @_; + my $min_c = 0; + for my $c (0 .. $#row) { + if ($row[$c] < $row[$min_c]) { + $min_c = $c; + } + } + return $min_c; +} + +sub max_row_in_col { + my($c, @matrix) = @_; + my $max_r = 0; + for my $r (0 .. $#matrix) { + if ($matrix[$r][$c] > $matrix[$max_r][$c]) { + $max_r = $r; + } + } + return $max_r; +} diff --git a/challenge-251/paulo-custodio/t/test-1.yaml b/challenge-251/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..77e3ea9def --- /dev/null +++ b/challenge-251/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 6 12 25 1 + input: + output: 1286 +- setup: + cleanup: + args: 10 7 31 5 2 2 + input: + output: 489 +- setup: + cleanup: + args: 1 2 10 + input: + output: 112 diff --git a/challenge-251/paulo-custodio/t/test-2.yaml b/challenge-251/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a30d74bcc9 --- /dev/null +++ b/challenge-251/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: "'[ [ 3, 7, 8], [ 9, 11, 13], [15, 16, 17] ]'" + input: + output: 15 +- setup: + cleanup: + args: "'[ [ 1, 10, 4, 2], [ 9, 3, 8, 7], [15, 16, 17, 12] ]'" + input: + output: 12 +- setup: + cleanup: + args: "'[ [7 ,8], [1 ,2] ]'" + input: + output: 7 |
