diff options
| -rw-r--r-- | challenge-251/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-251/bob-lied/perl/ch-1.pl | 65 | ||||
| -rw-r--r-- | challenge-251/bob-lied/perl/ch-2.pl | 107 | ||||
| -rw-r--r-- | challenge-251/lubos-kolouch/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-251/lubos-kolouch/perl/ch-2.pl | 26 | ||||
| -rw-r--r-- | challenge-251/lubos-kolouch/python/ch-1.py | 23 | ||||
| -rw-r--r-- | challenge-251/lubos-kolouch/python/ch-2.py | 32 | ||||
| -rw-r--r-- | challenge-251/lubos-kolouch/raku/ch-1.raku | 26 | ||||
| -rw-r--r-- | challenge-251/lubos-kolouch/raku/ch-2.raku | 20 | ||||
| -rw-r--r-- | challenge-251/wambash/elixir/ch-1.exs | 45 |
10 files changed, 378 insertions, 3 deletions
diff --git a/challenge-251/bob-lied/README b/challenge-251/bob-lied/README index a315d04ddf..1fb5d8a320 100644 --- a/challenge-251/bob-lied/README +++ b/challenge-251/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 250 by Bob Lied +Solutions to weekly challenge 251 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-250/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-250/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-251/bob-lied diff --git a/challenge-251/bob-lied/perl/ch-1.pl b/challenge-251/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..2433c765fa --- /dev/null +++ b/challenge-251/bob-lied/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-1.pl Perl Weekly Challenge 251 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. +# 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 v5.38; +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub cval(@ints) +{ + my $val = 0; + $val += ( shift(@ints) . (pop(@ints) // "") ) while @ints; + return $val; +} + +sub runTest +{ + use Test2::V0; + + is( cval(6,12,25,1), 1286, "Example 1"); + is( cval(10,7,31,5,2,2), 489, "Example 2"); + is( cval(1,2,10), 112, "Example 3"); + + done_testing; +} diff --git a/challenge-251/bob-lied/perl/ch-2.pl b/challenge-251/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..db22577ce1 --- /dev/null +++ b/challenge-251/bob-lied/perl/ch-2.pl @@ -0,0 +1,107 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-2.pl Perl Weekly Challenge 251 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. +# 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 v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub extremeIndex($row, $comparator) +{ + my $max = $row->[0]; + my $maxI = 0; + while ( my ($i, $val) = each(@$row) ) + { + ($maxI, $max) = ($i, $val) if ( $comparator->($val, $max) ); + } + return $maxI; +} +sub maxIndex($row) { extremeIndex($row, sub { $_[0] > $_[1] }); } +sub minIndex($row) { extremeIndex($row, sub { $_[0] < $_[1] }); } + +sub column($m, $col) { [ map { $_->[$col] } $m->@* ]; } + +sub lucky($matrix) +{ + my @max = map { minIndex($_) } $matrix->@*; + for my $rowNum ( 0 .. $#max ) + { + if ( maxIndex( column($matrix, $max[$rowNum])) == $rowNum ) + { + return $matrix->[$rowNum][$max[$rowNum]]; + } + } + return -1; +} + +sub runTest +{ + use Test2::V0; + + is( maxIndex([4,5,6]), 2, "maxIndex last"); + is( maxIndex([6,4,2]), 0, "maxIndex first"); + is( maxIndex([6,9,2]), 1, "maxIndex middle"); + is( maxIndex([7 ]), 0, "maxIndex singleton"); + + is( minIndex([6,5,4]), 2, "minIndex last"); + is( minIndex([2,4,6]), 0, "minIndex first"); + is( minIndex([6,1,2]), 1, "minIndex middle"); + is( minIndex([7 ]), 0, "minIndex singleton"); + + my @matrix = ( [1,2,3], [4,5,6] ); + is( column(\@matrix, 0), [1,4], "column 0"); + is( column(\@matrix, 1), [2,5], "column 1"); + is( column(\@matrix, 2), [3,6], "column 2"); + + my @case = ( [ "Example 1", 15, [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ], + ], + [ "Example 2", 12, [ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ], + ], + [ "Example 3", 7, [ [7 ,8], + [1 ,2] ], + ], + [ "No such luck", -1, [ [ 3, 6, 9 ], + [ 9, 15, 21 ], + [ 17, 16, 15] ], + ], +); + for my $tc ( @case ) + { + is( lucky($tc->[2]), $tc->[1], $tc->[0] ); + } + + done_testing; +} 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 diff --git a/challenge-251/wambash/elixir/ch-1.exs b/challenge-251/wambash/elixir/ch-1.exs new file mode 100644 index 0000000000..1153cee416 --- /dev/null +++ b/challenge-251/wambash/elixir/ch-1.exs @@ -0,0 +1,45 @@ +defmodule Concatenation do + def iter([]) do + nil + end + + def iter([o]) do + {o, []} + end + + def iter([f | t]) do + with [l | t] <- Enum.reverse(t) do + {String.to_integer("#{f}#{l}"), Enum.reverse(t)} + end + end + + def value(ints) do + ints + |> Stream.unfold(&iter/1) + |> Enum.sum() + end +end + +ExUnit.start() + +defmodule ConcatenationTest do + use ExUnit.Case + + test "concatenation value" do + assert Concatenation.iter([6, 12, 25, 1]) == {61, [12, 25]} + assert Concatenation.iter([12, 25]) == {1225, []} + assert Concatenation.iter([]) == nil + assert Concatenation.value([6, 12, 25, 1]) == 1286 + end + + test "concatenation value more elems" do + assert Concatenation.iter([10, 7, 31, 5, 2, 2]) == {102, [7, 31, 5, 2]} + assert Concatenation.value([10, 7, 31, 5, 2, 2]) == 489 + end + + test "concatenation value odd elems" do + assert Concatenation.iter([1, 2, 10]) == {110, [2]} + assert Concatenation.iter([2]) == {2, []} + assert Concatenation.value([1, 2, 10]) == 112 + end +end |
