From ae2d092f1d47bcf5a5c4e622ae2d7117e76dfc8a Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 14 Jan 2024 14:22:57 +0100 Subject: feat(challenge-251/lubos-kolouch/perl,python,raku/): Challenge 251 LK Perl Python Raku --- challenge-251/lubos-kolouch/perl/ch-1.pl | 31 +++++++++++++++++++++++++++++ challenge-251/lubos-kolouch/perl/ch-2.pl | 26 ++++++++++++++++++++++++ challenge-251/lubos-kolouch/python/ch-1.py | 23 +++++++++++++++++++++ challenge-251/lubos-kolouch/python/ch-2.py | 32 ++++++++++++++++++++++++++++++ challenge-251/lubos-kolouch/raku/ch-1.raku | 26 ++++++++++++++++++++++++ challenge-251/lubos-kolouch/raku/ch-2.raku | 20 +++++++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 challenge-251/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-251/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-251/lubos-kolouch/python/ch-1.py create mode 100644 challenge-251/lubos-kolouch/python/ch-2.py create mode 100644 challenge-251/lubos-kolouch/raku/ch-1.raku create mode 100644 challenge-251/lubos-kolouch/raku/ch-2.raku diff --git a/challenge-251/lubos-kolouch/perl/ch-1.pl b/challenge-251/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..a655b020d1 --- /dev/null +++ b/challenge-251/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; + +sub concatenation_value { + my @ints = @_; + my $concat_value = 0; + while (@ints) { + if (@ints == 1) { + $concat_value += $ints[0]; + shift @ints; + } + else { + my $first = $ints[0]; + my $last = $ints[-1]; + my $concat = "$first$last"; + $concat_value += $concat; + shift @ints; + pop @ints; + } + } + return $concat_value; +} + +# Tests +use Test::More; + +is(concatenation_value(6, 12, 25, 1), 1286, "Example 1"); +is(concatenation_value(10, 7, 31, 5, 2, 2), 489, "Example 2"); +is(concatenation_value(1, 2, 10), 112, "Example 3"); + +done_testing(); diff --git a/challenge-251/lubos-kolouch/perl/ch-2.pl b/challenge-251/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..df1afdcb61 --- /dev/null +++ b/challenge-251/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; +use List::Util qw(min); +use List::MoreUtils qw(first_index all); + +sub lucky_number { + my @matrix = @_; + for my $i (0..$#matrix) { + my $min_row = min(@{$matrix[$i]}); + my $min_index = first_index { $_ == $min_row } @{$matrix[$i]}; + if (all { $matrix[$_][$min_index] <= $min_row } 0..$#matrix) { + return $min_row; + } + } + return -1; +} + +# Example usage +my @matrix1 = ([3, 7, 8], [9, 11, 13], [15, 16, 17]); +print lucky_number(@matrix1) . "\n"; # Output: 15 + +my @matrix2 = ([1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]); +print lucky_number(@matrix2) . "\n"; # Output: 12 + +my @matrix3 = ([7, 8], [1, 2]); +print lucky_number(@matrix3) . "\n"; # Output: 7 diff --git a/challenge-251/lubos-kolouch/python/ch-1.py b/challenge-251/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..62a2fcd678 --- /dev/null +++ b/challenge-251/lubos-kolouch/python/ch-1.py @@ -0,0 +1,23 @@ +from typing import List + + +def concatenation_value(ints: list[int]) -> int: + concat_value = 0 + while len(ints) > 0: + if len(ints) == 1: + concat_value += ints[0] + del ints[0] + else: + first = str(ints[0]) + last = str(ints[-1]) + concat = int(first + last) + concat_value += concat + del ints[0] + del ints[-1] + return concat_value + + +# Tests +assert concatenation_value([6, 12, 25, 1]) == 1286 +assert concatenation_value([10, 7, 31, 5, 2, 2]) == 489 +assert concatenation_value([1, 2, 10]) == 112 diff --git a/challenge-251/lubos-kolouch/python/ch-2.py b/challenge-251/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..8983f5e6f0 --- /dev/null +++ b/challenge-251/lubos-kolouch/python/ch-2.py @@ -0,0 +1,32 @@ +from typing import List + + +def lucky_number(matrix: list[list[int]]) -> int: + """ + Returns the lucky number in the given matrix, 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. + + Args: + matrix: A list of lists of integers representing the matrix. + + Returns: + An integer representing the lucky number, or -1 if there is no lucky number in the matrix. + """ + for i in range(len(matrix)): + min_row = min(matrix[i]) + min_index = matrix[i].index(min_row) + if all(matrix[j][min_index] <= min_row for j in range(len(matrix))): + return min_row + return -1 + + +# Example usage +matrix1 = [[3, 7, 8], [9, 11, 13], [15, 16, 17]] +print(lucky_number(matrix1)) # Output: 15 + +matrix2 = [[1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]] +print(lucky_number(matrix2)) # Output: 12 + +matrix3 = [[7, 8], [1, 2]] +print(lucky_number(matrix3)) # Output: 7 diff --git a/challenge-251/lubos-kolouch/raku/ch-1.raku b/challenge-251/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..73d095e0fb --- /dev/null +++ b/challenge-251/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,26 @@ +sub concatenation-value(@ints where .all ~~ Int) returns Int { + my $concat-value = 0; + while @ints { + if @ints == 1 { + $concat-value += @ints[0]; + @ints.shift; + } + else { + my $first = @ints[0]; + my $last = @ints[*-1]; + my $concat = "$first$last".Int; + $concat-value += $concat; + @ints.shift; + @ints.pop; + } + } + return $concat-value; +} + +# Tests +use Test; +plan 3; + +is concatenation-value([6, 12, 25, 1]), 1286, "Example 1"; +is concatenation-value([10, 7, 31, 5, 2, 2]), 489, "Example 2"; +is concatenation-value([1, 2, 10]), 112, "Example 3"; diff --git a/challenge-251/lubos-kolouch/raku/ch-2.raku b/challenge-251/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..e27ffb34c4 --- /dev/null +++ b/challenge-251/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,20 @@ +sub lucky-number(@matrix) { + for @matrix.kv -> $i, @row { + my $min-row = @row.min; + my $min-index = @row.first($min-row, :k); + if all(@matrix.map(*[$min-index])) <= $min-row { + return $min-row; + } + } + return -1; +} + +# Example usage +my @matrix1 = ([3, 7, 8], [9, 11, 13], [15, 16, 17]); +say lucky-number(@matrix1); # Output: 15 + +my @matrix2 = ([1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]); +say lucky-number(@matrix2); # Output: 12 + +my @matrix3 = ([7, 8], [1, 2]); +say lucky-number(@matrix3); # Output: 7 -- cgit