diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2024-02-06 10:57:00 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2024-02-06 10:57:00 +0800 |
| commit | 1bf81c8cd7a349e23f5cea03031f16d11af36f83 (patch) | |
| tree | adde1cb7a839ec7f3691bfc4bfd494636ed2503e /challenge-253 | |
| parent | 409a0635fc8d10d342a7b6dd72c1c23a2c0702e0 (diff) | |
| parent | cde7d521095de2ce582570f6d8d1a9e7bbcec16c (diff) | |
| download | perlweeklychallenge-club-1bf81c8cd7a349e23f5cea03031f16d11af36f83.tar.gz perlweeklychallenge-club-1bf81c8cd7a349e23f5cea03031f16d11af36f83.tar.bz2 perlweeklychallenge-club-1bf81c8cd7a349e23f5cea03031f16d11af36f83.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-253')
| -rw-r--r-- | challenge-253/luca-ferrari/pljava/ch-1.java | 40 | ||||
| -rw-r--r-- | challenge-253/luca-ferrari/pljava/ch-2.java | 28 | ||||
| -rw-r--r-- | challenge-253/massa/raku/ch-1.raku | 59 | ||||
| -rw-r--r-- | challenge-253/massa/raku/ch-2.raku | 87 |
4 files changed, 214 insertions, 0 deletions
diff --git a/challenge-253/luca-ferrari/pljava/ch-1.java b/challenge-253/luca-ferrari/pljava/ch-1.java new file mode 100644 index 0000000000..51a2428a5b --- /dev/null +++ b/challenge-253/luca-ferrari/pljava/ch-1.java @@ -0,0 +1,40 @@ +//package PWC253; + +/** + * PL/Java implementation for PWC 253 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> + * + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC253.1.jar', 'PWC253', true ); + select sqlj.set_classpath( 'public', 'PWC253' ); + + select task$tn_pljava(); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; + +public class Task1 { + + @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE ) + public static final String[] task1_pljava( String separator, String[] words ) { + List<String> result = new LinkedList<String>(); + + for ( String w : words ) { + if ( ! w.contains( separator ) ) + result.add( w ); + else + result.addAll( w.split( separator ) ); + } + + return result; + + } +} diff --git a/challenge-253/luca-ferrari/pljava/ch-2.java b/challenge-253/luca-ferrari/pljava/ch-2.java new file mode 100644 index 0000000000..4a664fb345 --- /dev/null +++ b/challenge-253/luca-ferrari/pljava/ch-2.java @@ -0,0 +1,28 @@ +package PWC253; + +/** + * PL/Java implementation for PWC 253 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> + * + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC253.1.jar', 'PWC253', true ); + select sqlj.set_classpath( 'public', 'PWC253' ); + + select task$tn_pljava(); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +public class Task2 { + + @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE ) + public static final void task2_pljava() throws Exception { + throws Exception( "Not Implemented" ); + } +} diff --git a/challenge-253/massa/raku/ch-1.raku b/challenge-253/massa/raku/ch-1.raku new file mode 100644 index 0000000000..6b665ca6a6 --- /dev/null +++ b/challenge-253/massa/raku/ch-1.raku @@ -0,0 +1,59 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 1: Split Strings + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of strings and a character separator. + +Write a script to return all words separated by the given character excluding +empty string. + +=head3 Example 1: + + Input: @words = ("one.two.three","four.five","six") + $separator = "." + Output: "one","two","three","four","five","six" + +=head3 Example 2: + + Input: @words = ("$perl$$", "$$raku$") + $separator = "$" + Output: "perl","raku" + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION($sep, @_) { + @_.map(*.split($sep, :skip-empty).Slip) +} + +multi MAIN (Bool :$test!) { + use Testo; + + my @tests = + %{ input => \( '.', ( 'one.two.three', 'four.five', 'six' ) ), + output => ( "one","two","three","four","five","six" ) }, + %{ input => \( '$', ( '$perl$$', '$$raku$' ) ), + output => ( 'perl', 'raku' ) }, + ; + + SOLUTION(|.<input>).&is: .<output>, .<text> for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-253/massa/raku/ch-2.raku b/challenge-253/massa/raku/ch-2.raku new file mode 100644 index 0000000000..838c5bf822 --- /dev/null +++ b/challenge-253/massa/raku/ch-2.raku @@ -0,0 +1,87 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Weakest Rows + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0. + +A row i is weaker than a row j if one of the following is true: + + a) The number of 1s in row i is less than the number of 1s in row j. + b) Both rows have the same number of 1 and i < j. + +Write a script to return the order of rows from weakest to strongest. + +=head3 Example 1: + + Input: $matrix = [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ] + Output: (2, 0, 3, 1, 4) + + The number of 1s in each row is: + - Row 0: 2 + - Row 1: 4 + - Row 2: 1 + - Row 3: 2 + - Row 4: 5 + +=head3 Example 2: + + Input: $matrix = [ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ] + Output: (0, 2, 3, 1) + + The number of 1s in each row is: + - Row 0: 1 + - Row 1: 4 + - Row 2: 1 + - Row 3: 1 + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION(@matrix) { + @matrix».sum.pairs.sort( { $^a.value <=> $^b.value || $^a.key <=> $^b.key } )».key +} + +multi MAIN (Bool :$test!) { + use Testo; + + my @tests = + %{ input => [ + [1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1] + ], output => (2, 0, 3, 1, 4) }, + %{ input => [ + [1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0] + ], output => (0, 2, 3, 1) }, + ; + + .<input>.&SOLUTION.&is: .<output>, .<text> for @tests +} # end of multi MAIN (Bool :$test!) + + |
