From cf099618b9fb98ac225c348e4c42e19b79dd67ab Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 8 Jan 2024 07:01:43 +0000 Subject: Challenge 251 Solutions (Raku) --- challenge-251/mark-anderson/raku/ch-1.raku | 11 ++++++ challenge-251/mark-anderson/raku/ch-2.raku | 55 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 challenge-251/mark-anderson/raku/ch-1.raku create mode 100644 challenge-251/mark-anderson/raku/ch-2.raku diff --git a/challenge-251/mark-anderson/raku/ch-1.raku b/challenge-251/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..6b938ecd27 --- /dev/null +++ b/challenge-251/mark-anderson/raku/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku +use Test; + +is concat-val([6,12,25,1]), 1286; +is concat-val([10,7,31,5,2,2]), 489; +is concat-val([1,2,10]), 112; + +sub concat-val(@a) +{ + [+] map { @a.shift ~ (@a.pop || Empty) }, ^(@a / 2) +} diff --git a/challenge-251/mark-anderson/raku/ch-2.raku b/challenge-251/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..eede360155 --- /dev/null +++ b/challenge-251/mark-anderson/raku/ch-2.raku @@ -0,0 +1,55 @@ +#!/usr/bin/env raku +use Test; + +is-deeply (15,), lucky-numbers([ + [ 3, 7, 8 ], + [ 9, 11, 13 ], + [ 15, 16, 17 ] + ]); + +is-deeply (12,), lucky-numbers([ + [ 1, 10, 4, 2 ], + [ 9, 3, 8, 7 ], + [ 15, 16, 17, 12 ] + ]); + +is-deeply (7,), lucky-numbers([ + [ 7, 8 ], + [ 1, 2 ] + ]); + +is-deeply (16,), lucky-numbers([ + [ 22, 4, 20, 24 ], + [ 21, 16, 23, 18 ], + [ 3, 12, 19, 5 ], + [ 15, 7, 10, 2 ], + [ 17, 11, 6, 13 ], + [ 9, 8, 14, 1 ] + ]); + +is-deeply (), lucky-numbers([ + [ 26, 50, 10, 54, 42, 31, 45, 58 ], + [ 29, 64, 23, 35, 15, 41, 27, 17 ], + [ 55, 37, 1, 30, 52, 53, 21, 46 ], + [ 3, 2, 6, 36, 11, 13, 19, 22 ], + [ 61, 47, 32, 39, 49, 62, 9, 12 ], + [ 20, 60, 44, 7, 59, 34, 14, 16 ], + [ 8, 24, 38, 43, 56, 48, 57, 25 ], + [ 5, 40, 33, 28, 63, 18, 4, 51 ] + ]); + +is-deeply (43,), lucky-numbers([ + [ 34, 2, 9, 53, 32, 29, 36, 30 ], + [ 7, 19, 24, 10, 26, 27, 31, 25 ], + [ 52, 50, 39, 11, 22, 1, 54, 62 ], + [ 45, 56, 59, 20, 37, 17, 40, 42 ], + [ 14, 8, 63, 46, 5, 44, 61, 3 ], + [ 38, 15, 18, 16, 6, 12, 60, 4 ], + [ 41, 23, 13, 21, 35, 33, 28, 51 ], + [ 58, 48, 49, 55, 43, 47, 64, 57 ] + ]); + +sub lucky-numbers(@m) +{ + keys (@m>>.min (&) ([Z] @m)>>.max) +} -- cgit From 2d8482e6a6c66e41be970509f8b1b25afe0df9aa Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 8 Jan 2024 07:15:12 +0000 Subject: Challenge 251 Solutions (Raku) --- challenge-251/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-251/mark-anderson/raku/ch-2.raku b/challenge-251/mark-anderson/raku/ch-2.raku index eede360155..635e8aa611 100644 --- a/challenge-251/mark-anderson/raku/ch-2.raku +++ b/challenge-251/mark-anderson/raku/ch-2.raku @@ -51,5 +51,5 @@ is-deeply (43,), lucky-numbers([ sub lucky-numbers(@m) { - keys (@m>>.min (&) ([Z] @m)>>.max) + keys @m>>.min (&) ([Z] @m)>>.max } -- cgit From 460fd6dd4635df456cb82978c5ab1e4b925deae0 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 8 Jan 2024 19:35:52 +1100 Subject: pwc251 solution in python --- challenge-251/pokgopun/python/ch-1.py | 88 +++++++++++++++++++++++++++++++++++ challenge-251/pokgopun/python/ch-2.py | 78 +++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 challenge-251/pokgopun/python/ch-1.py create mode 100644 challenge-251/pokgopun/python/ch-2.py diff --git a/challenge-251/pokgopun/python/ch-1.py b/challenge-251/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..41fd8c1c0c --- /dev/null +++ b/challenge-251/pokgopun/python/ch-1.py @@ -0,0 +1,88 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +""" + +Task 1: Concatenation Value + +Submitted by: [52]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 + +Task 2: Lucky Numbers +""" +### solution by pokgopun@gmail.com + +def concatenation(tup: tuple): + #print(tup) + l = len(tup) + sum = 0 + for i in range(l//2): + #print(tup[i],tup[l-1-i],"=>",int(str(tup[i])+str(tup[l-1-i]))) + sum += int(str(tup[i])+str(tup[l-1-i])) + if l % 2: + #print("addition",tup[l//2]) + sum += tup[l//2] + return sum + +import unittest + +class TestConcatenation(unittest.TestCase): + def test(self): + for inpt,otpt in { + (6, 12, 25, 1): 1286, + (10, 7, 31, 5, 2, 2): 489, + (1, 2, 10): 112, + }.items(): + self.assertEqual(concatenation(inpt),otpt) + +unittest.main() + + + diff --git a/challenge-251/pokgopun/python/ch-2.py b/challenge-251/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..c987d9f4df --- /dev/null +++ b/challenge-251/pokgopun/python/ch-2.py @@ -0,0 +1,78 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +""" + +Task 2: Lucky Numbers + +Submitted by: [53]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 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 14th January + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def luckynumber(tup: list): + #print(tup) + res = -1 + for i in range(len(tup)): + mn = min(tup[i]) + #print("mn =",mn) + j = tup[i].index(mn) + if mn == max(e[j] for e in tup): + res = mn + break + return res + +import unittest + +class TestLuckynumber(unittest.TestCase): + def test(self): + for inpt,otpt in { + ( ( 3, 7, 8), + ( 9, 11, 13), + (15, 16, 17) ): 15, + ( ( 1, 10, 4, 2), + ( 9, 3, 8, 7), + (15, 16, 17, 12) ): 12, + ( (7 ,8), + (1 ,2) ): 7, + }.items(): + self.assertEqual(luckynumber(inpt),otpt) + +unittest.main() + + -- cgit From 275425a6539c092ed817bba98f9bf3bf910f4897 Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 8 Jan 2024 10:10:08 +0000 Subject: Challenge 251 --- challenge-251/simon-proctor/raku/ch-1.raku | 28 +++++++++++++++++++++ challenge-251/simon-proctor/raku/ch-2.raku | 40 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 challenge-251/simon-proctor/raku/ch-1.raku create mode 100644 challenge-251/simon-proctor/raku/ch-2.raku diff --git a/challenge-251/simon-proctor/raku/ch-1.raku b/challenge-251/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..a85d9e7171 --- /dev/null +++ b/challenge-251/simon-proctor/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +#| Run Test Suite +multi sub MAIN('test') { + use Test; + is concat-number(6, 12, 25, 1), 1286; + is concat-number(10, 7, 31, 5, 2, 2), 489; + is concat-number(1, 2, 10), 112; + done-testing; +} + +#| Print the concatentaion number +multi sub MAIN( + *@vals where all(@vals) ~~ IntStr #= List of ints +) { + concat-number( |@vals ).say; +} + +sub concat-number( *@vals ) { + my $total = 0; + while (@vals.elems > 1) { + my $f = @vals.shift; + my $l = @vals.pop; + $total += ($f ~ $l); + } + $total += @vals[0] if @vals.elems == 1; + return $total; +} diff --git a/challenge-251/simon-proctor/raku/ch-2.raku b/challenge-251/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..e5f95f5216 --- /dev/null +++ b/challenge-251/simon-proctor/raku/ch-2.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku + +#| Run the Test Suite +multi sub MAIN('test') { + use Test; + is-deeply rot-matrix([[1,2],[3,4]]), [[1,3],[2,4]]; + is lucky( [[3,7,8],[9,11,13],[15,16,17]] ), 15; + is lucky( [[1,10,4,2],[9,3,8,7],[15,16,17,12]] ), 12; + is lucky( [[7,8],[1,2]] ), 7; + is lucky( [[1,2],[3,4]] ), 3; + done-testing; +} + +multi sub MAIN( + Int $width, #= Matrix width + *@vals where @vals.unique.elems == @vals.elems && all(@vals) ~~ IntStr, #= Matrix values +) { + my @matrix = @vals.rotor($width); + lucky(@matrix).say; +} + +sub rot-matrix( @matrix ) { + my @out; + for (^@matrix.elems) -> $i { + for (^@matrix[0].elems) -> $j { + @out[$j] //= []; + @out[$j][$i] = @matrix[$i][$j]; + } + } + return @out; +} + +sub lucky( @matrix ) { + my @rot = rot-matrix(@matrix); + my @mins = @matrix.map( *.min ); + my @maxs = @rot.map( *.max ); + my $int = @mins ∩ @maxs; + return -1 unless $int.keys.elems == 1; + return $int.keys[0]; +} -- cgit From 9ba7d42d06bfcfa6762c442611c9d84a01a422ca Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 8 Jan 2024 10:11:42 +0000 Subject: Challenge 251 (Small updateto part 2) --- challenge-251/simon-proctor/raku/ch-2.raku | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/challenge-251/simon-proctor/raku/ch-2.raku b/challenge-251/simon-proctor/raku/ch-2.raku index e5f95f5216..17ae44be46 100644 --- a/challenge-251/simon-proctor/raku/ch-2.raku +++ b/challenge-251/simon-proctor/raku/ch-2.raku @@ -13,7 +13,9 @@ multi sub MAIN('test') { multi sub MAIN( Int $width, #= Matrix width - *@vals where @vals.unique.elems == @vals.elems && all(@vals) ~~ IntStr, #= Matrix values + *@vals where @vals.unique.elems == @vals.elems #= Matrix values + && all(@vals) ~~ IntStr + && @vals.elems %% $width, ) { my @matrix = @vals.rotor($width); lucky(@matrix).say; -- cgit From 080fde56fb9825f48e63c97840234fbde25fedaa Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 8 Jan 2024 10:17:22 +0100 Subject: PWC 251 Task 1 Raku done Task 2 Raku done Task 1 PL/Perl done Task 2 PL/perl done Task 1 PL/PgSQL done Task 2 PL/PgSQL done Task 1 Python done Task 2 Python done --- challenge-251/luca-ferrari/blog-1.txt | 1 + challenge-251/luca-ferrari/blog-2.txt | 1 + challenge-251/luca-ferrari/blog-3.txt | 1 + challenge-251/luca-ferrari/blog-4.txt | 1 + challenge-251/luca-ferrari/blog-5.txt | 1 + challenge-251/luca-ferrari/blog-6.txt | 1 + challenge-251/luca-ferrari/blog-7.txt | 1 + challenge-251/luca-ferrari/blog-8.txt | 1 + challenge-251/luca-ferrari/postgresql/ch-1.plperl | 23 +++++++++ challenge-251/luca-ferrari/postgresql/ch-1.sql | 29 +++++++++++ challenge-251/luca-ferrari/postgresql/ch-2.plperl | 63 +++++++++++++++++++++++ challenge-251/luca-ferrari/postgresql/ch-2.sql | 16 ++++++ challenge-251/luca-ferrari/python/ch-1.py | 25 +++++++++ challenge-251/luca-ferrari/python/ch-2.py | 59 +++++++++++++++++++++ challenge-251/luca-ferrari/raku/ch-1.p6 | 17 ++++++ challenge-251/luca-ferrari/raku/ch-2.p6 | 37 +++++++++++++ 16 files changed, 277 insertions(+) create mode 100644 challenge-251/luca-ferrari/blog-1.txt create mode 100644 challenge-251/luca-ferrari/blog-2.txt create mode 100644 challenge-251/luca-ferrari/blog-3.txt create mode 100644 challenge-251/luca-ferrari/blog-4.txt create mode 100644 challenge-251/luca-ferrari/blog-5.txt create mode 100644 challenge-251/luca-ferrari/blog-6.txt create mode 100644 challenge-251/luca-ferrari/blog-7.txt create mode 100644 challenge-251/luca-ferrari/blog-8.txt create mode 100644 challenge-251/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-251/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-251/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-251/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-251/luca-ferrari/python/ch-1.py create mode 100644 challenge-251/luca-ferrari/python/ch-2.py create mode 100644 challenge-251/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-251/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-251/luca-ferrari/blog-1.txt b/challenge-251/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..ef5631ba42 --- /dev/null +++ b/challenge-251/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task1 diff --git a/challenge-251/luca-ferrari/blog-2.txt b/challenge-251/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..40f36701a6 --- /dev/null +++ b/challenge-251/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task2 diff --git a/challenge-251/luca-ferrari/blog-3.txt b/challenge-251/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..d8cf826c69 --- /dev/null +++ b/challenge-251/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task1plperl diff --git a/challenge-251/luca-ferrari/blog-4.txt b/challenge-251/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..8bc8154b8b --- /dev/null +++ b/challenge-251/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task2plperl diff --git a/challenge-251/luca-ferrari/blog-5.txt b/challenge-251/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..e8326a85ec --- /dev/null +++ b/challenge-251/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task1plpgsql diff --git a/challenge-251/luca-ferrari/blog-6.txt b/challenge-251/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..57b87c236c --- /dev/null +++ b/challenge-251/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task2plpgsql diff --git a/challenge-251/luca-ferrari/blog-7.txt b/challenge-251/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..9437b6564e --- /dev/null +++ b/challenge-251/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task1python diff --git a/challenge-251/luca-ferrari/blog-8.txt b/challenge-251/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..f653f319b0 --- /dev/null +++ b/challenge-251/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task2python diff --git a/challenge-251/luca-ferrari/postgresql/ch-1.plperl b/challenge-251/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..7152e60c8a --- /dev/null +++ b/challenge-251/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,23 @@ +-- +-- Perl Weekly Challenge 251 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc251; + +CREATE OR REPLACE FUNCTION +pwc251.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $nums ) = @_; + my $sum = 0; + + for ( 0 .. $nums->@* - 2 ) { + next if $_ % 2 != 0; + $sum += $nums->@[ $_ ] . $nums->@[ $_ + 1 ]; + } + + return $sum; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-251/luca-ferrari/postgresql/ch-1.sql b/challenge-251/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..83c215c5f6 --- /dev/null +++ b/challenge-251/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 251 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc251; + +CREATE OR REPLACE FUNCTION +pwc251.task1_plpgsql( nums int[] ) +RETURNS int +AS $CODE$ +DECLARE + s text; + v int := 0; +BEGIN + FOR i IN 1 .. array_length( nums, 1 ) - 1 LOOP + IF i % 2 = 0 THEN + CONTINUE; + END IF; + + v := v + ( nums[ i ]::text || nums[ i + 1 ]::text )::int; + END LOOP; + + RETURN v; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-251/luca-ferrari/postgresql/ch-2.plperl b/challenge-251/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..32d43ba6ef --- /dev/null +++ b/challenge-251/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,63 @@ +-- +-- Perl Weekly Challenge 251 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc251; + +/** + * example + testdb=> select pwc251.task2_plperl( array[ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17]] ); + task2_plperl +-------------- + 15 +(1 row) +*/ +CREATE OR REPLACE FUNCTION +pwc251.task2_plperl( int[][] ) +RETURNS int +AS $CODE$ + my ( $matrix ) = @_; + + my $max_col = undef; + + # search the max column value + my @max_col_indexes; + for my $col ( 0 .. $matrix->@[ 0 ]->@* - 1 ) { + $max_col_index[ $col ] = undef; + + for my $row ( 0 .. $matrix->@* - 1 ) { + $max_col_index[ $col ] = $matrix->@[ $row ]->[ $col ] if ( ! $max_col_index[ $col ] || $max_col_index[ $col ] < $matrix->@[ $row ]->[ $col ] ); + } + } + for my $row ( 0 .. $matrix->@* - 1 ) { + for my $col ( 0 .. $matrix->@[ $row ]->@* - 1 ) { + $max_col = $matrix->@[ $row ]->[ $col ] if ( ! $max_col || $max_col < $matrix->@[ $row ]->[ $col ] ); + } + } + + for my $row ( 0 .. $matrix->@* - 1 ) { + my $current_min = undef; + my $current_min_index = 0; + + for my $col ( 0 .. $matrix->@[ $row ]->@* - 1 ) { + if ( ! $current_min || $matrix->@[ $row ]->[ $col ] < $current_min ) { + $current_min = $matrix->@[ $row ]->[ $col ]; + $current_min_index = $col; + } + } + + if ( $current_min == $max_col_index[ $current_min_index ] ) { + return $current_min; + } + + + } + + return -1; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-251/luca-ferrari/postgresql/ch-2.sql b/challenge-251/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..fcdf6a6f9a --- /dev/null +++ b/challenge-251/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,16 @@ +-- +-- Perl Weekly Challenge 251 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc251; + +CREATE OR REPLACE FUNCTION +pwc251.task2_plpgsql( matrix int[][] ) +RETURNS int +AS $CODE$ + SELECT pwc251.task2_plperl( matrix ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-251/luca-ferrari/python/ch-1.py b/challenge-251/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..e72f979066 --- /dev/null +++ b/challenge-251/luca-ferrari/python/ch-1.py @@ -0,0 +1,25 @@ +#!python + +# +# Perl Weekly Challenge 251 +# Task 1 +# +# See +# + +import sys + +# task implementation +def main( argv ): + sum = 0 + for i in range( 0, len( argv ) - 1 ): + if i % 2 != 0: + continue + sum += int( argv[ i ] + argv[ i + 1 ] ) + + return sum + + +# invoke the main without the command itself +if __name__ == '__main__': + print( main( sys.argv[ 1: ] ) ) diff --git a/challenge-251/luca-ferrari/python/ch-2.py b/challenge-251/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..a06c706b28 --- /dev/null +++ b/challenge-251/luca-ferrari/python/ch-2.py @@ -0,0 +1,59 @@ +#!python + +# +# Perl Weekly Challenge 251 +# Task 2 +# +# See +# + +import sys + +# task implementation +def main( argv ): + matrix = [] + row = 0 + matrix.append( [] ) + + for x in argv[ 0 ]: + if x == ' ': + continue + + # change row + if x == '|': + row += 1 + matrix.append( [] ) + continue + + matrix[ row ].append( int( x ) ) + + # make indexes of max columns + max_cols = [] + for col in range( 0, len( matrix ) ): + current_max = None + for row in range( 0, len( matrix[ 0 ] ) ): + if not current_max or current_max < matrix[ row ][ col ] : + current_max = matrix[ row ][ col ] + + max_cols.append( current_max ) + + for row in range( 0, len( matrix ) ): + current_min = None + current_min_col = None + + for col in range( 0, len( matrix[ row ] ) ): + if not current_min or current_min > matrix[ row ][ col ]: + current_min = matrix[ row ][ col ] + current_min_col = col + + if current_min == max_cols[ current_min_col ]: + return current_min + + return -1 + + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( main( sys.argv[ 1: ] ) ) diff --git a/challenge-251/luca-ferrari/raku/ch-1.p6 b/challenge-251/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..c0ba780273 --- /dev/null +++ b/challenge-251/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 251 +# Task 1 +# +# See +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems and @nums.elems %% 2 } ) { + my $sum = 0; + for @nums -> $l, $r { + $sum += $l ~ $r; + } + + $sum.say; +} diff --git a/challenge-251/luca-ferrari/raku/ch-2.p6 b/challenge-251/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..cbfdd4baab --- /dev/null +++ b/challenge-251/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,37 @@ +#!raku + +# +# Perl Weekly Challenge 251 +# Task 2 +# +# See +# + +sub MAIN() { + + my $matrix = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]; + + my @max-col; + for 0 ..^ $matrix[ 0 ].elems -> $col { + my @current-col.push: $matrix[ $_ ][ $col ] for 0 ..^ $matrix.elems; + @max-col[ $col ] = @current-col.max; + } + + for 0 ..^ $matrix.elems -> $row { + my $min = Nil; + my $min-col = Nil; + + for 0 ..^ $matrix[ $row ].elems -> $col { + if ( ! $min || $matrix[ $row ][ $col ] < $min ) { + $min-col = $col; + $min = $matrix[ $row ][ $col ]; + } + } + + $min.say and exit if ( @max-col[ $min-col ] == $min ); + } + + '-1'.say; +} -- cgit From 2f0103fc32a161b83ebfbc34b7ccce5d26200d45 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 8 Jan 2024 11:31:26 +0100 Subject: Add solution 251 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-251/jeanluc2020/blog-1.txt | 1 + challenge-251/jeanluc2020/blog-2.txt | 1 + challenge-251/jeanluc2020/perl/ch-1.pl | 95 ++++++++++++++++++++++++++++++++ challenge-251/jeanluc2020/perl/ch-2.pl | 95 ++++++++++++++++++++++++++++++++ challenge-251/jeanluc2020/python/ch-1.py | 87 +++++++++++++++++++++++++++++ challenge-251/jeanluc2020/python/ch-2.py | 82 +++++++++++++++++++++++++++ 6 files changed, 361 insertions(+) create mode 100644 challenge-251/jeanluc2020/blog-1.txt create mode 100644 challenge-251/jeanluc2020/blog-2.txt create mode 100755 challenge-251/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-251/jeanluc2020/perl/ch-2.pl create mode 100755 challenge-251/jeanluc2020/python/ch-1.py create mode 100755 challenge-251/jeanluc2020/python/ch-2.py diff --git a/challenge-251/jeanluc2020/blog-1.txt b/challenge-251/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..26c059d208 --- /dev/null +++ b/challenge-251/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-251-1.html diff --git a/challenge-251/jeanluc2020/blog-2.txt b/challenge-251/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..9816093212 --- /dev/null +++ b/challenge-251/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-251-2.html diff --git a/challenge-251/jeanluc2020/perl/ch-1.pl b/challenge-251/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..64648b97c2 --- /dev/null +++ b/challenge-251/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK1 +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# Starting with a sum of 0, we call a function that just concatenates +# the first and last elements of @ints and adds that number to the +# current sum, then we call the function recursively with the new +# current sum and the remainder of the array. Of course handle the +# case where there is only one element in the array by returning the +# current sum + the last element, and the case of an empty array by +# returning the current sum. + +use strict; +use warnings; + +concatenation_value(6, 12, 25, 1); +concatenation_value(10, 7, 31, 5, 2, 2); +concatenation_value(1, 2, 10); + +sub concatenation_value { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $result = concatenation_value_(0, @ints); + print "Output: $result\n"; +} + +sub concatenation_value_ { + my ($current, @ints) = @_; + return $current unless @ints; + if(@ints >= 2) { + my $first = shift @ints; + my $last = pop @ints; + return concatenation_value_($current + "$first$last", @ints); + } else { + return $current + $ints[0]; + } +} diff --git a/challenge-251/jeanluc2020/perl/ch-2.pl b/challenge-251/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..6c41933c38 --- /dev/null +++ b/challenge-251/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK2 +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# For a lucky number, we know: +# Since the minimum in a row is at the same time the maximum in +# a column, there is no number bigger in that column, so in all +# other rows, there is a number that is smaller than the current +# number, so the minimum in that other row has to be smaller or +# equal to this number; being smaller, it can't be bigger than any +# of the numbers in the original row, so it's can't be a lucky number +# as well. +# So we know we have at most one lucky number. +# Finding it is simple: In each row, find the minimum number, then +# check whether it is as well the maximum number in its column. + +use strict; +use warnings; +use List::Util qw(min max); + +lucky_numbers([ [ 3, 7, 8], [ 9, 11, 13], [15, 16, 17] ]); +lucky_numbers([ [ 1, 10, 4, 2], [ 9, 3, 8, 7], [15, 16, 17, 12] ]); +lucky_numbers([ [7 ,8], [1 ,2] ]); +lucky_numbers([ [7 ,8], [10 ,2] ]); + +sub lucky_numbers { + my $matrix = shift; + print "Input: [\n"; + foreach my $row (@$matrix) { + print " [" . join(", ", @$row) . "],\n"; + } + print " ];\n"; + my $rows = scalar(@$matrix); + my $columns = scalar(@{$matrix->[0]}); + my $row_min = []; + my $column_max = []; + foreach my $column (0..$columns-1) { + my @column_elements = (); + foreach my $row (0..$rows-1) { + push @column_elements, $matrix->[$row]->[$column]; + } + push @$column_max, max(@column_elements); + } + foreach my $i (0..$rows-1) { + push @$row_min, min(@{$matrix->[$i]}); + foreach my $j (0..$columns-1) { + my $elem = $matrix->[$i]->[$j]; + if($elem == $row_min->[$i] && $elem == $column_max->[$j]) { + print "Output: $elem\n"; + return; + } + } + } + print "Output: -1\n"; +} + + diff --git a/challenge-251/jeanluc2020/python/ch-1.py b/challenge-251/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..9f0b248134 --- /dev/null +++ b/challenge-251/jeanluc2020/python/ch-1.py @@ -0,0 +1,87 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK1 +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# Starting with a sum of 0, we call a function that just concatenates +# the first and last elements of @ints and adds that number to the +# current sum, then we call the function recursively with the new +# current sum and the remainder of the array. Of course handle the +# case where there is only one element in the array by returning the +# current sum + the last element, and the case of an empty array by +# returning the current sum. + +def concatenation_value_(current: int, ints: list) -> int: + if len(ints) == 0: + return current + if len(ints) == 1: + return current + ints[0] + value = str(ints[0]) + str(ints[-1]) + return concatenation_value_(current + int(value), ints[1:-1]) + +def concatenation_value(ints: list) -> None: + print("Input: (", ", ".join([str(x) for x in ints]), ")") + result = concatenation_value_(0, ints) + print(f"Output: {result}") + +concatenation_value([6, 12, 25, 1]) +concatenation_value([10, 7, 31, 5, 2, 2]) +concatenation_value([1, 2, 10]) + diff --git a/challenge-251/jeanluc2020/python/ch-2.py b/challenge-251/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..33dd88684d --- /dev/null +++ b/challenge-251/jeanluc2020/python/ch-2.py @@ -0,0 +1,82 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK2 +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# For a lucky number, we know: +# Since the minimum in a row is at the same time the maximum in +# a column, there is no number bigger in that column, so in all +# other rows, there is a number that is smaller than the current +# number, so the minimum in that other row has to be smaller or +# equal to this number; being smaller, it can't be bigger than any +# of the numbers in the original row, so it's can't be a lucky number +# as well. +# So we know we have at most one lucky number. +# Finding it is simple: In each row, find the minimum number, then +# check whether it is as well the maximum number in its column. + +def lucky_numbers(matrix: list) -> None: + print("Input: ["); + for row in matrix: + print(" [", ", ".join([str(x) for x in row]), "],") + print(" ];") + rows = len(matrix) + columns = len(matrix[0]) + row_min = [] + column_max = [] + for column in range(columns): + column_elements = [] + for row in range(rows): + column_elements.append(matrix[row][column]) + column_max.append(max(column_elements)) + for i in range(rows): + row_min.append(min(matrix[i])) + for j in range(columns): + elem = matrix[i][j] + if elem == row_min[i] and elem == column_max[j]: + print(f"Output: {elem}") + return + print("Output: -1") + +lucky_numbers([ [ 3, 7, 8], [ 9, 11, 13], [15, 16, 17] ]) +lucky_numbers([ [ 1, 10, 4, 2], [ 9, 3, 8, 7], [15, 16, 17, 12] ]) +lucky_numbers([ [7 ,8], [1 ,2] ]) +lucky_numbers([ [7 ,8], [10 ,2] ]) + -- cgit From 87a1cd6110b045d9095085a87d46515abd8a2da4 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 8 Jan 2024 22:58:40 +1100 Subject: pwc251 solution in go --- challenge-251/pokgopun/go/ch-1.go | 119 ++++++++++++++++++++++++++++++++++++++ challenge-251/pokgopun/go/ch-2.go | 110 +++++++++++++++++++++++++++++++++++ 2 files changed, 229 insertions(+) create mode 100644 challenge-251/pokgopun/go/ch-1.go create mode 100644 challenge-251/pokgopun/go/ch-2.go diff --git a/challenge-251/pokgopun/go/ch-1.go b/challenge-251/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..f74e5e089d --- /dev/null +++ b/challenge-251/pokgopun/go/ch-1.go @@ -0,0 +1,119 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +/*# + +Task 1: Concatenation Value + +Submitted by: [52]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 + +Task 2: Lucky Numbers +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) concat() int { + l := len(is) + switch l { + case 0: + return 0 + case 1: + return is[0] + default: + c := is[0] + for i := 1; i < l; i++ { + lim := is[i] + for lim > 0 { + lim /= 10 + c *= 10 + } + c += is[i] + } + return c + } +} + +func (is ints) concatenation() int { + l := len(is) + lim := l / 2 + c := 0 + for i := 0; i < lim; i++ { + //fmt.Println(ints{is[i], is[l-1-i]}.concat()) + c += ints{is[i], is[l-1-i]}.concat() + } + if l%2 == 1 { + c += is[lim] + } + return c +} + +func main() { + for _, data := range []struct { + input ints + output int + }{ + {ints{6, 12, 25, 1}, 1286}, + {ints{10, 7, 31, 5, 2, 2}, 489}, + {ints{1, 2, 10}, 112}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.concatenation(), data.output)) // display nothing if ok, otherwise show the difference + } +} diff --git a/challenge-251/pokgopun/go/ch-2.go b/challenge-251/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..f4dfec84ea --- /dev/null +++ b/challenge-251/pokgopun/go/ch-2.go @@ -0,0 +1,110 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +/*# + +Task 2: Lucky Numbers + +Submitted by: [53]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 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 14th January + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type row []int + +func (rw row) minIdx() int { + l := len(rw) + switch l { + case 0: + return -1 + case 1: + return 0 + default: + j := 0 + for i, v := range rw[1:] { + if rw[j] > v { + j = i + 1 + } + } + return j + } +} + +type matrix []row + +func (mt matrix) lucky() int { + l := len(mt) + switch l { + case 1: + return mt[0][mt[0].minIdx()] + default: + for _, rw := range mt { + i := rw.minIdx() + mx := mt[0][i] + for _, v := range mt[1:] { + mx = max(mx, v[i]) + } + if mx == rw[i] { + return mx + } + } + } + return -1 +} + +func main() { + for _, data := range []struct { + input matrix + output int + }{ + {matrix{row{3, 7, 8}, row{9, 11, 13}, row{15, 16, 17}}, 15}, + {matrix{row{1, 10, 4, 2}, row{9, 3, 8, 7}, row{15, 16, 17, 12}}, 12}, + {matrix{row{7, 8}, row{1, 2}}, 7}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.lucky(), data.output)) // display nothing if ok, otherwise show the difference + } +} -- cgit From 21209f0c085ea3916f95ba72f0f867dcae1343a7 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:54:47 +0000 Subject: Challenge 251 Solutions (Raku) --- challenge-251/mark-anderson/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-251/mark-anderson/raku/ch-1.raku b/challenge-251/mark-anderson/raku/ch-1.raku index 6b938ecd27..59d0543404 100644 --- a/challenge-251/mark-anderson/raku/ch-1.raku +++ b/challenge-251/mark-anderson/raku/ch-1.raku @@ -7,5 +7,5 @@ is concat-val([1,2,10]), 112; sub concat-val(@a) { - [+] map { @a.shift ~ (@a.pop || Empty) }, ^(@a / 2) + sum do while @a { @a.shift ~ (@a.pop || Empty) } } -- cgit From 383ceb7778a970f4cd188c5f0bf27f1ea685b21a Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 8 Jan 2024 14:57:52 +0000 Subject: Week 251 ... --- challenge-251/peter-campbell-smith/blog.txt | 1 + challenge-251/peter-campbell-smith/perl/ch-1.pl | 38 ++++++++ challenge-251/peter-campbell-smith/perl/ch-2.pl | 117 ++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 challenge-251/peter-campbell-smith/blog.txt create mode 100755 challenge-251/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-251/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-251/peter-campbell-smith/blog.txt b/challenge-251/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..4dce7e403d --- /dev/null +++ b/challenge-251/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/251 diff --git a/challenge-251/peter-campbell-smith/perl/ch-1.pl b/challenge-251/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..478f3c5960 --- /dev/null +++ b/challenge-251/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2024-01-08 +use utf8; # Week 251 task 1 - Concatenation value +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +binmode STDOUT, ':utf8'; +my ($j, @ints); + +concatenation_value(6, 12, 25, 1); +concatenation_value(10, 7, 31, 5, 2, 2); +concatenation_value(1, 2, 10); + +for $j (0 .. 99) { + push(@ints, int(rand(99))); +} +concatenation_value(@ints); + +sub concatenation_value { + + my(@ints, $sum, $j, $k); + + # initialise + @ints = @_; + $sum = 0; + + # iterate from both ends + for $j (0 .. @ints) { + $k = @ints - $j - 1; + last if $k < $j; + $sum += ($k == $j ? $ints[$j] : $ints[$j] . $ints[$k]); + } + + # show result + say qq[\nInput: (] . join(', ', @ints) . ')'; + say qq[Output: $sum]; +} diff --git a/challenge-251/peter-campbell-smith/perl/ch-2.pl b/challenge-251/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..d8d37a3015 --- /dev/null +++ b/challenge-251/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,117 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2024-01-08 +use utf8; # Week 251 task 2 - Lucky numbers +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +binmode STDOUT, ':utf8'; + +lucky_numbers([[ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17]]); + +lucky_numbers([[ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12]]); + +lucky_numbers([[7, 8], + [1, 2]]); + +lucky_numbers([[1, 2, 3], + [2, 1, 4], + [3, 4, 1]]); + +lucky_numbers([[7, 4, 4, 7], + [6, 5, 5, 6], + [6, 5, 5, 6], + [7, 4, 4, 7]]); + + +sub lucky_numbers { + + my ($r, $c, @lucky); + our ($m, @mins, @maxs); + @mins = @maxs = (); + + $m = shift; + + # loop over elements of matrix + for $r (0 .. @$m - 1) { + for $c (0 .. @{$m->[0]} - 1) { + + # save value if element is lucky + push(@lucky, $m->[$r]->[$c]) if (min_in_row($r, $c) and max_in_col($r, $c)); + } + } + say format_matrix(qq{\nInput: \@matrix = }, $m); + say qq[Output: ] . (@lucky > 0 ? join(', ', @lucky) : -1); + + sub min_in_row { + + my ($r, $c, $cx, $min_value); + + # return true/false if element $m($r, $c) is the minimum in its row + ($r, $c) = @_; + + # return saved value if we've been here before + return ($mins[$r] == $m->[$r]->[$c] ? 1 : 0) if defined $mins[$r]; + + # or determine and save it if not + $min_value = 99999; + for $cx (0 .. @{$m->[0]} - 1) { + $min_value = $m->[$r]->[$cx] if $m->[$r]->[$cx] < $min_value; + } + $mins[$r] = $min_value; + return $m->[$r]->[$c] == $min_value; + } + + sub max_in_col { + + my ($r, $c, $rx, $max_value); + + # return true/false if element $m($r, $c) is the maximum in its column + ($r, $c) = @_; + + # return saved value if we've been here before + return ($maxs[$c] == $m->[$r]->[$c] ? 1 : 0) if defined $maxs[$c]; + + # or determine and save it if not + $max_value = -99999; + for $rx (0 .. @$m - 1) { + $max_value = $m->[$rx]->[$c] if $m->[$rx]->[$c] > $max_value; + } + $maxs[$c] = $max_value; + return $m->[$r]->[$c] == $max_value; + } +} + +sub format_matrix { + + # format the output + my ($w, $m, $r, $c, $prefix, $width, $rubric, $spaces, $line); + + ($rubric, $m) = @_; + $spaces = length($rubric); + + # find maximum width of element as printed by Perl + $w = 0; + for $r (0 .. @$m - 1) { + for $c (0. .. @{$m->[0]} - 1) { + $width = length($m->[$r]->[$c]); + $w = $width if $width > $w; + } + } + + # construct and output each row of matrix + for $r (0 .. @$m - 1) { + $line = $rubric . '['; + for $c (0 .. @{$m->[0]} - 1) { + $line .= sprintf("%${w}d", $m->[$r]->[$c]) . ', '; + } + $line =~ s|, $|]|; + print $line; + say '' unless $r == @$m - 1; + $rubric = (' ' x ($spaces - 1)); + } +} -- cgit From 9193caa0085c4e69bdec566035803303ca1ad328 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 8 Jan 2024 15:12:17 +0000 Subject: w251 - Task 1 & 2 --- challenge-251/perlboy1967/perl/ch1.pl | 41 ++++++++++++++++++++++++++++++ challenge-251/perlboy1967/perl/ch2.pl | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100755 challenge-251/perlboy1967/perl/ch1.pl create mode 100755 challenge-251/perlboy1967/perl/ch2.pl diff --git a/challenge-251/perlboy1967/perl/ch1.pl b/challenge-251/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..57f97135fc --- /dev/null +++ b/challenge-251/perlboy1967/perl/ch1.pl @@ -0,0 +1,41 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 251 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-251 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::Util qw(sum0); + +sub concatenationValue (@ints) { + # splice in an extra 0 if the list has odd number of elements + splice(@ints,$#ints/2-1,1,$ints[$#ints/2-1],0) if ($#ints % 2 == 0); + my $i = 0; + sum0 map { "$ints[$_]$ints[--$i]" } 0 .. $#ints/2; +} + +is(concatenationValue(6,12,25,1),1286); +is(concatenationValue(10,7,31,5,2,2),489); +is(concatenationValue(1,2,10),112); + +done_testing; diff --git a/challenge-251/perlboy1967/perl/ch2.pl b/challenge-251/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..60cd0517ed --- /dev/null +++ b/challenge-251/perlboy1967/perl/ch2.pl @@ -0,0 +1,48 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 251 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-251 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::Util qw(min max); +use List::MoreUtils qw(duplicates); + +sub luckyNumber ($ar) { + my @min = map { min @{$$ar[$_]} } 0 .. $#$ar; + my @max = map { my $c = $_; + max map { $$ar[$_][$c] } 0 .. $#$ar + } 0 .. $#{$ar->[0]}; + (duplicates @min,@max)[0]; +} + +is(luckyNumber([ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]),15); +is(luckyNumber([ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ]),12); +is(luckyNumber([ [7 ,8], + [1 ,2] ]), 7); + +done_testing; -- cgit From 598422b9745ea871f161f2dcbfd883380ab706b1 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 8 Jan 2024 09:30:01 -0600 Subject: SOlve PWC251 --- challenge-251/wlmb/blog.txt | 1 + challenge-251/wlmb/perl/ch-1.pl | 14 ++++++++++++++ challenge-251/wlmb/perl/ch-2.pl | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 challenge-251/wlmb/blog.txt create mode 100755 challenge-251/wlmb/perl/ch-1.pl create mode 100755 challenge-251/wlmb/perl/ch-2.pl diff --git a/challenge-251/wlmb/blog.txt b/challenge-251/wlmb/blog.txt new file mode 100644 index 0000000000..3c2c1d1653 --- /dev/null +++ b/challenge-251/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2024/01/08/PWC251/ diff --git a/challenge-251/wlmb/perl/ch-1.pl b/challenge-251/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..828c183030 --- /dev/null +++ b/challenge-251/wlmb/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +# Perl weekly challenge 251 +# Task 1: Concatenation Value +# +# See https://wlmb.github.io/2024/01/08/PWC251/#task-1-concatenation-value +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to concatenate and add the numbers N_1 . N_k + N_2 . N_{k-1} +... + FIN +my $tot=0; +my @ints=@ARGV; +$tot += shift(@ints).(pop(@ints)//"") while(@ints); +say "@ARGV -> $tot"; diff --git a/challenge-251/wlmb/perl/ch-2.pl b/challenge-251/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..d835b0f63a --- /dev/null +++ b/challenge-251/wlmb/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +# Perl weekly challenge 251 +# Task 2: Lucky Numbers +# +# See https://wlmb.github.io/2024/01/08/PWC251/#task-2-lucky-numbers +use v5.36; +use PDL; +die <<~"FIN" unless @ARGV; + Usage: $0 A1 [A2...] + to find lucky numbers in the arrays A1, A2... + The format of the arrays should be "[[a00 a01...],[a10 a11...],...]", + to be read by pdl. + FIN +while(@ARGV){ + my $in=pdl(shift); + my $min=$in->minover->dummy(0); + my $max=$in->mv(1,0)->maxover->dummy(1); + my $result=$in->where(($in==$min)&($in==$max)); + say "$in -> ", $result->isempty?-1:$result; +} -- cgit From 71232d40036d0663b1fb1f8b84f63823075f0b68 Mon Sep 17 00:00:00 2001 From: Zapwai Date: Mon, 8 Jan 2024 11:38:16 -0500 Subject: Week 251 --- challenge-251/zapwai/c/ch-1.c | 54 +++++++++++++++++++++++++++ challenge-251/zapwai/c/ch-2.c | 77 +++++++++++++++++++++++++++++++++++++++ challenge-251/zapwai/perl/ch-1.pl | 16 ++++++++ challenge-251/zapwai/perl/ch-2.pl | 30 +++++++++++++++ challenge-251/zapwai/rust/ch-1.rs | 25 +++++++++++++ challenge-251/zapwai/rust/ch-2.rs | 57 +++++++++++++++++++++++++++++ 6 files changed, 259 insertions(+) create mode 100644 challenge-251/zapwai/c/ch-1.c create mode 100644 challenge-251/zapwai/c/ch-2.c create mode 100644 challenge-251/zapwai/perl/ch-1.pl create mode 100644 challenge-251/zapwai/perl/ch-2.pl create mode 100644 challenge-251/zapwai/rust/ch-1.rs create mode 100644 challenge-251/zapwai/rust/ch-2.rs diff --git a/challenge-251/zapwai/c/ch-1.c b/challenge-251/zapwai/c/ch-1.c new file mode 100644 index 0000000000..81c6bc263a --- /dev/null +++ b/challenge-251/zapwai/c/ch-1.c @@ -0,0 +1,54 @@ +#include +#include +void proc(int*, int); +int main() { + int arr0[] = {6, 12, 25, 1}; + int arr1[] = {10, 7, 31, 5, 2, 2}; + int arr2[] = {1,2,10}; + int L[3] = {sizeof(arr0)/sizeof(int), + sizeof(arr1)/sizeof(int), + sizeof(arr2)/sizeof(int), + }; + printf("\n"); + proc(arr0, L[0]); + printf("\t-=-=-\n"); + proc(arr1, L[1]); + printf("\t-=-=-\n"); + proc(arr2, L[2]); + printf("\n"); +} + +void proc(int* arr, int L) { + int sum = 0; + int k = 0; + if (L % 2 == 1) { + k = L/2; + sum += arr[k]; + } else { + k = L/2 - 1; + } + + for (int i = 0; i <= k; i++) { + int a; + if (L % 2 == 1) { + if (i == k) { + break; + } + a = arr[k-i-1]; + } else { + a = arr[k-i]; + } + int b = arr[k+1+i]; + char x[20]; + sprintf(x, "%d%d", a,b); + int X = atoi(x); + // printf("%d\n",X); + sum += X; + } + printf("Input: arr = {"); + for (int i = 0; i < L - 1; i++) { + printf("%d, ",arr[i]); + } + printf("%d}\n",arr[L-1]); + printf("Output: %d\n",sum); +} diff --git a/challenge-251/zapwai/c/ch-2.c b/challenge-251/zapwai/c/ch-2.c new file mode 100644 index 0000000000..f605e9724c --- /dev/null +++ b/challenge-251/zapwai/c/ch-2.c @@ -0,0 +1,77 @@ +#include +void proc(int m,int n, int[m][n]); + +void ex1() { + const int M = 3; + const int N = 3; + int a[M][N] = { { 3, 7, 8}, + { 9, 11, 13}, + {15, 16, 17} }; + proc(M,N,a); +} + +void ex2() { + const int M = 3; + const int N = 4; + int a[M][N] = { { 1, 10, 4, 2}, + { 9, 3, 8, 7}, + {15, 16, 17, 12} }; + proc(M,N,a); +} + +void ex3() { + const int M = 2; + const int N = 2; + int a[M][N] = { { 7, 8}, + {1, 2} }; + proc(M,N,a); +} + +int main() { + ex1(); + ex2(); + ex3(); +} + +void proc(int M, int N, int a[M][N]) { + int mins[M]; + int maxs[N]; + + for (int i = 0; i < M; i++) { + int min = a[i][0]; + for (int j = 0; j < N; j++) { + int x = a[i][j]; + (min > x) ? min = x : 0; + } + mins[i] = min; + } + + for (int j = 0; j < N; j++) { + int max = 0; + for (int i = 0; i < M; i++) { + int x = a[i][j]; + (max < x) ? max = x : 0; + + } + maxs[j] = max; + } + + int ans = -1; + for (int i = 0; i < M; i++) { + for (int j = 0; j < N; j++) { + if (mins[i] == maxs[j]) { + ans = mins[i]; + goto over; + } + } + } + over: + printf("Input: M = \n\t"); + for (int i = 0; i < M; i++) { + for (int j = 0; j < N; j++) { + printf("%02d ", a[i][j]); + } + printf("\n\t"); + } + printf("\nOutput: %d\n", ans); +} diff --git a/challenge-251/zapwai/perl/ch-1.pl b/challenge-251/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..17b06a9cb9 --- /dev/null +++ b/challenge-251/zapwai/perl/ch-1.pl @@ -0,0 +1,16 @@ +use v5.30; +my @ints = (6, 12, 25, 1); +say "Input: \@ints = (" . join(", ", @ints) . ")"; +my $sum = 0; +do { + proc(\@ints); +} while (@ints); +say "Output: $sum"; +sub proc { + my $r = shift; + my $a = shift @$r; + my $b; + $b = pop @$r if (@$r) ; + my $x = $a.$b; + $sum += $x; +} diff --git a/challenge-251/zapwai/perl/ch-2.pl b/challenge-251/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..e7cad53007 --- /dev/null +++ b/challenge-251/zapwai/perl/ch-2.pl @@ -0,0 +1,30 @@ +use v5.30; +use List::Util qw( max min ); +my $matrix = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17], + ]; +say "Input: \$matrix = ["; +say "\t[@$_]" foreach (@$matrix); +say "\t];"; +my $h = @$matrix; +my $w = scalar @{$$matrix[0]}; +my $luck = -1; +my @min; +for my $i (0 .. $h - 1) { + push @min, min (@{$$matrix[$i]}); +} +my @max; +for my $j (0 .. $w - 1) { + my @col = map { $$matrix[$_][$j] } (0 .. $h - 1) ; + push @max, max @col; +} +loop: for my $val (@min) { + for my $val2 (@max) { + if ($val == $val2) { + $luck = $val; + last loop; + } + } +} +say "Output: $luck"; diff --git a/challenge-251/zapwai/rust/ch-1.rs b/challenge-251/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..64d08e61ee --- /dev/null +++ b/challenge-251/zapwai/rust/ch-1.rs @@ -0,0 +1,25 @@ +fn main() { + let ints = vec![6, 12, 25, 1]; + let ints2 = vec![10, 7, 31, 5, 2, 2]; + let ints3 = vec![1, 2, 10]; + proc(ints); + proc(ints2); + proc(ints3); +} + +fn proc (mut ints : Vec) { + println!("Input: ints = {:?}", ints); + let mut sum = 0; + while ints.len() > 1 + { + let a = ints.remove(0); + let b = ints.pop().unwrap(); + let x = format!("{:?}{:?}",a,b); + sum += x.parse::().unwrap(); + + } + if ints.len() == 1 { + sum += ints[0]; + } + println!("Output: {sum}"); +} diff --git a/challenge-251/zapwai/rust/ch-2.rs b/challenge-251/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..94f1cdd15a --- /dev/null +++ b/challenge-251/zapwai/rust/ch-2.rs @@ -0,0 +1,57 @@ +fn main() { + let a = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]; + + // let a = [ [ 1, 10, 4, 2], + // [ 9, 3, 8, 7], + // [15, 16, 17, 12] ]; + + // let a = [ [7 ,8], + // [1 ,2] ]; + + let w :usize = a[0].len(); + let h :usize = a.len(); + // proc(w, h, a); + // } + + // fn proc(w: usize, h: usize, a: [[i32;3];3]) { + let mut mins = Vec::new(); + let mut maxs = Vec::new(); + + let mut j = 0; + while j <= h-1 { + mins.push(a[j].iter().min().unwrap()); + j += 1; + } + let mut i = 0; + while i <= w-1 { + let mut max = -1; + j = 0; + while j <= h-1 { + let x = a[j][i]; + if max < x { + max = x; + } + j += 1; + } + maxs.push(max); + i += 1; + } + + let mut luck = -1; + 'outer: for i in mins { + for j in &maxs { + if i == j { + luck = *j; + break 'outer; + } + } + } + println!("Input: "); + for i in 0 .. h { + + println!("\t{:?}", a[i]); + } + println!("Output: {luck}"); +} -- cgit From bac8160bd6d93442b20aef4e8b5d396939b55c42 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 8 Jan 2024 12:42:39 -0500 Subject: DAJ #251 --- challenge-251/dave-jacoby/blog.txt | 1 + challenge-251/dave-jacoby/perl/ch-1.pl | 38 ++++++++++++++++++++ challenge-251/dave-jacoby/perl/ch-2.pl | 63 ++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 challenge-251/dave-jacoby/blog.txt create mode 100644 challenge-251/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-251/dave-jacoby/perl/ch-2.pl diff --git a/challenge-251/dave-jacoby/blog.txt b/challenge-251/dave-jacoby/blog.txt new file mode 100644 index 0000000000..61c31e441a --- /dev/null +++ b/challenge-251/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2024/01/08/do-you-feel-lucky-weekly-challenge-251.html diff --git a/challenge-251/dave-jacoby/perl/ch-1.pl b/challenge-251/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..3d144f59f4 --- /dev/null +++ b/challenge-251/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + [ 6, 12, 25, 1 ], + [ 10, 7, 31, 5, 2, 2 ], + [ 1, 2, 10 ], +); + +for my $example (@examples) { + my $input = join ', ', $example->@*; + my $output = kinda_concatenation( $example->@* ); + + say <<~"END"; + Input: \$ints = ($input) + Output: $output + END +} + +sub kinda_concatenation (@input) { + my @output; + while (@input) { + if ( scalar @input == 1 ) { + push @output, shift @input; + } + else { +