From e7b355008ec9f641223eb4165cc1d0452216040d Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Tue, 21 Jan 2025 18:00:17 +0000 Subject: - Added solutions by Eric Cheung. - Added solutions by Richar Park. - Added solutions by PokGoPun. - Added solutions by Neils van Dijke. - Added solutions by Mark Anderson. - Added solutions by E. Choroba. - Added solutions by Luca Ferrari. - Added solutions by Peter Campbell Smith. - Added solutions by Conor Hoekstra. - Added solutions by David Ferrone. - Added solutions by Robbie Hatley. - Added solutions by W. Luis Mochan. - Added solutions by Feng Chang. - Added solutions by Roger Bell_West. --- challenge-305/conor-hoekstra/bqn/ch-1.bqn | 12 + challenge-305/conor-hoekstra/bqn/ch-2.bqn | 13 + challenge-305/conor-hoekstra/ch-1.bqn | 12 - challenge-305/conor-hoekstra/ch-2.bqn | 13 - challenge-305/eric-cheung/python/ch-1.py | 16 + challenge-305/eric-cheung/python/ch-2.py | 34 + challenge-305/luca-ferrari/blog-1.txt | 1 + challenge-305/luca-ferrari/blog-10.txt | 1 + challenge-305/luca-ferrari/blog-2.txt | 1 + challenge-305/luca-ferrari/blog-3.txt | 1 + challenge-305/luca-ferrari/blog-4.txt | 1 + challenge-305/luca-ferrari/blog-5.txt | 1 + challenge-305/luca-ferrari/blog-6.txt | 1 + challenge-305/luca-ferrari/blog-7.txt | 1 + challenge-305/luca-ferrari/blog-8.txt | 1 + challenge-305/luca-ferrari/blog-9.txt | 1 + challenge-305/luca-ferrari/pljava/pom.xml | 72 + .../luca-ferrari/pljava/src/main/java/Task1.java | 81 + .../luca-ferrari/pljava/src/main/java/Task2.java | 69 + challenge-305/luca-ferrari/plperl/ch-1.plperl | 35 + challenge-305/luca-ferrari/plperl/ch-2.plperl | 31 + challenge-305/luca-ferrari/plpgsql/ch-1.sql | 56 + challenge-305/luca-ferrari/plpgsql/ch-2.sql | 15 + challenge-305/luca-ferrari/python/ch-1.py | 37 + challenge-305/luca-ferrari/python/ch-2.py | 30 + challenge-305/luca-ferrari/raku/ch-1.raku | 19 + challenge-305/luca-ferrari/raku/ch-2.raku | 17 + challenge-305/perlboy1967/perl/ch-1.pl | 43 + challenge-305/perlboy1967/perl/ch-2.pl | 46 + challenge-305/perlboy1967/perl/ch1.pl | 43 - challenge-305/perlboy1967/perl/ch2.pl | 46 - .../richard-park/apl/AlienDictionary.aplf | 7 + challenge-305/richard-park/apl/BinaryPrefix.aplf | 6 + challenge-305/richard-park/apl/W305_APL.ipynb | 450 +++++ challenge-305/zapwai/ch-1.pl | 25 - challenge-305/zapwai/ch-2.pl | 45 - challenge-305/zapwai/perl/ch-1.pl | 25 + challenge-305/zapwai/perl/ch-2.pl | 45 + challenge-306/luca-ferrari/blog-1.txt | 1 - challenge-306/luca-ferrari/blog-10.txt | 1 - challenge-306/luca-ferrari/blog-2.txt | 1 - challenge-306/luca-ferrari/blog-3.txt | 1 - challenge-306/luca-ferrari/blog-4.txt | 1 - challenge-306/luca-ferrari/blog-5.txt | 1 - challenge-306/luca-ferrari/blog-6.txt | 1 - challenge-306/luca-ferrari/blog-7.txt | 1 - challenge-306/luca-ferrari/blog-8.txt | 1 - challenge-306/luca-ferrari/blog-9.txt | 1 - challenge-306/luca-ferrari/pljava/pom.xml | 72 - .../luca-ferrari/pljava/src/main/java/Task1.java | 81 - .../luca-ferrari/pljava/src/main/java/Task2.java | 69 - challenge-306/luca-ferrari/plperl/ch-1.plperl | 35 - challenge-306/luca-ferrari/plperl/ch-2.plperl | 31 - challenge-306/luca-ferrari/plpgsql/ch-1.sql | 56 - challenge-306/luca-ferrari/plpgsql/ch-2.sql | 15 - challenge-306/luca-ferrari/python/ch-1.py | 37 - challenge-306/luca-ferrari/python/ch-2.py | 30 - challenge-306/luca-ferrari/raku/ch-1.raku | 19 - challenge-306/luca-ferrari/raku/ch-2.raku | 17 - stats/pwc-challenge-304.json | 483 +++++ stats/pwc-current.json | 473 +---- stats/pwc-language-breakdown-2019.json | 612 +++--- stats/pwc-language-breakdown-2020.json | 376 ++-- stats/pwc-language-breakdown-2021.json | 758 ++++---- stats/pwc-language-breakdown-2022.json | 398 ++-- stats/pwc-language-breakdown-2023.json | 396 ++-- stats/pwc-language-breakdown-2024.json | 416 ++-- stats/pwc-language-breakdown-2025.json | 113 +- stats/pwc-language-breakdown-summary.json | 52 +- stats/pwc-leaders.json | 478 ++--- stats/pwc-summary-1-30.json | 100 +- stats/pwc-summary-121-150.json | 44 +- stats/pwc-summary-151-180.json | 112 +- stats/pwc-summary-181-210.json | 110 +- stats/pwc-summary-211-240.json | 38 +- stats/pwc-summary-241-270.json | 42 +- stats/pwc-summary-271-300.json | 96 +- stats/pwc-summary-301-330.json | 34 +- stats/pwc-summary-31-60.json | 112 +- stats/pwc-summary-61-90.json | 114 +- stats/pwc-summary-91-120.json | 30 +- stats/pwc-summary.json | 1988 ++++++++++---------- stats/pwc-yearly-language-summary.json | 154 +- 83 files changed, 5054 insertions(+), 4300 deletions(-) create mode 100644 challenge-305/conor-hoekstra/bqn/ch-1.bqn create mode 100644 challenge-305/conor-hoekstra/bqn/ch-2.bqn delete mode 100644 challenge-305/conor-hoekstra/ch-1.bqn delete mode 100644 challenge-305/conor-hoekstra/ch-2.bqn create mode 100755 challenge-305/eric-cheung/python/ch-1.py create mode 100755 challenge-305/eric-cheung/python/ch-2.py create mode 100644 challenge-305/luca-ferrari/blog-1.txt create mode 100644 challenge-305/luca-ferrari/blog-10.txt create mode 100644 challenge-305/luca-ferrari/blog-2.txt create mode 100644 challenge-305/luca-ferrari/blog-3.txt create mode 100644 challenge-305/luca-ferrari/blog-4.txt create mode 100644 challenge-305/luca-ferrari/blog-5.txt create mode 100644 challenge-305/luca-ferrari/blog-6.txt create mode 100644 challenge-305/luca-ferrari/blog-7.txt create mode 100644 challenge-305/luca-ferrari/blog-8.txt create mode 100644 challenge-305/luca-ferrari/blog-9.txt create mode 100644 challenge-305/luca-ferrari/pljava/pom.xml create mode 100644 challenge-305/luca-ferrari/pljava/src/main/java/Task1.java create mode 100644 challenge-305/luca-ferrari/pljava/src/main/java/Task2.java create mode 100644 challenge-305/luca-ferrari/plperl/ch-1.plperl create mode 100644 challenge-305/luca-ferrari/plperl/ch-2.plperl create mode 100644 challenge-305/luca-ferrari/plpgsql/ch-1.sql create mode 100644 challenge-305/luca-ferrari/plpgsql/ch-2.sql create mode 100644 challenge-305/luca-ferrari/python/ch-1.py create mode 100644 challenge-305/luca-ferrari/python/ch-2.py create mode 100644 challenge-305/luca-ferrari/raku/ch-1.raku create mode 100644 challenge-305/luca-ferrari/raku/ch-2.raku create mode 100755 challenge-305/perlboy1967/perl/ch-1.pl create mode 100755 challenge-305/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-305/perlboy1967/perl/ch1.pl delete mode 100755 challenge-305/perlboy1967/perl/ch2.pl create mode 100644 challenge-305/richard-park/apl/AlienDictionary.aplf create mode 100644 challenge-305/richard-park/apl/BinaryPrefix.aplf create mode 100644 challenge-305/richard-park/apl/W305_APL.ipynb delete mode 100644 challenge-305/zapwai/ch-1.pl delete mode 100644 challenge-305/zapwai/ch-2.pl create mode 100644 challenge-305/zapwai/perl/ch-1.pl create mode 100644 challenge-305/zapwai/perl/ch-2.pl delete mode 100644 challenge-306/luca-ferrari/blog-1.txt delete mode 100644 challenge-306/luca-ferrari/blog-10.txt delete mode 100644 challenge-306/luca-ferrari/blog-2.txt delete mode 100644 challenge-306/luca-ferrari/blog-3.txt delete mode 100644 challenge-306/luca-ferrari/blog-4.txt delete mode 100644 challenge-306/luca-ferrari/blog-5.txt delete mode 100644 challenge-306/luca-ferrari/blog-6.txt delete mode 100644 challenge-306/luca-ferrari/blog-7.txt delete mode 100644 challenge-306/luca-ferrari/blog-8.txt delete mode 100644 challenge-306/luca-ferrari/blog-9.txt delete mode 100644 challenge-306/luca-ferrari/pljava/pom.xml delete mode 100644 challenge-306/luca-ferrari/pljava/src/main/java/Task1.java delete mode 100644 challenge-306/luca-ferrari/pljava/src/main/java/Task2.java delete mode 100644 challenge-306/luca-ferrari/plperl/ch-1.plperl delete mode 100644 challenge-306/luca-ferrari/plperl/ch-2.plperl delete mode 100644 challenge-306/luca-ferrari/plpgsql/ch-1.sql delete mode 100644 challenge-306/luca-ferrari/plpgsql/ch-2.sql delete mode 100644 challenge-306/luca-ferrari/python/ch-1.py delete mode 100644 challenge-306/luca-ferrari/python/ch-2.py delete mode 100644 challenge-306/luca-ferrari/raku/ch-1.raku delete mode 100644 challenge-306/luca-ferrari/raku/ch-2.raku create mode 100644 stats/pwc-challenge-304.json diff --git a/challenge-305/conor-hoekstra/bqn/ch-1.bqn b/challenge-305/conor-hoekstra/bqn/ch-1.bqn new file mode 100644 index 0000000000..311c35c960 --- /dev/null +++ b/challenge-305/conor-hoekstra/bqn/ch-1.bqn @@ -0,0 +1,12 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/305-1.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" +p ⇐ •Import "/home/cph/bqn-code/lib/primes.bqn" +b ⇐ •Import "/home/cph/bqn-code/lib/binary.bqn" + +BinaryPrefix ← p.IsPrime·b.Decimal¨∾` + +# Tests +u.UnitTest (BinaryPrefix 1‿0‿1) ≡ 0‿1‿1 +u.UnitTest (BinaryPrefix 1‿1‿0) ≡ 0‿1‿0 diff --git a/challenge-305/conor-hoekstra/bqn/ch-2.bqn b/challenge-305/conor-hoekstra/bqn/ch-2.bqn new file mode 100644 index 0000000000..67b1ac5aa0 --- /dev/null +++ b/challenge-305/conor-hoekstra/bqn/ch-2.bqn @@ -0,0 +1,13 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/305-2.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" + +AlienSort ← { 𝕩⊏˜⍋𝕨⊸⊐¨𝕩 } # Explicit +AlienSort2 ← ⍋∘(⊐˜¨⟜<)˜⊏⊢ # Tacit + +# Tests +u.UnitTest ("hlabydefgirkmnopqjstuvwxcz" AlienSort ⟨"perl", "python", "raku"⟩) ≡ ⟨"raku", "python", "perl"⟩ +u.UnitTest ("corldabtefghijkmnpqswuvxyz" AlienSort ⟨"the", "weekly", "challenge"⟩) ≡ ⟨"challenge", "the", "weekly"⟩ +u.UnitTest ("hlabydefgirkmnopqjstuvwxcz" AlienSort2 ⟨"perl", "python", "raku"⟩) ≡ ⟨"raku", "python", "perl"⟩ +u.UnitTest ("corldabtefghijkmnpqswuvxyz" AlienSort2 ⟨"the", "weekly", "challenge"⟩) ≡ ⟨"challenge", "the", "weekly"⟩ diff --git a/challenge-305/conor-hoekstra/ch-1.bqn b/challenge-305/conor-hoekstra/ch-1.bqn deleted file mode 100644 index 311c35c960..0000000000 --- a/challenge-305/conor-hoekstra/ch-1.bqn +++ /dev/null @@ -1,12 +0,0 @@ -# For up to date code: -# https://github.com/codereport/bqn-code/blob/main/pwc/305-1.bqn - -u ⇐ •Import "/home/cph/bqn-test/test.bqn" -p ⇐ •Import "/home/cph/bqn-code/lib/primes.bqn" -b ⇐ •Import "/home/cph/bqn-code/lib/binary.bqn" - -BinaryPrefix ← p.IsPrime·b.Decimal¨∾` - -# Tests -u.UnitTest (BinaryPrefix 1‿0‿1) ≡ 0‿1‿1 -u.UnitTest (BinaryPrefix 1‿1‿0) ≡ 0‿1‿0 diff --git a/challenge-305/conor-hoekstra/ch-2.bqn b/challenge-305/conor-hoekstra/ch-2.bqn deleted file mode 100644 index 67b1ac5aa0..0000000000 --- a/challenge-305/conor-hoekstra/ch-2.bqn +++ /dev/null @@ -1,13 +0,0 @@ -# For up to date code: -# https://github.com/codereport/bqn-code/blob/main/pwc/305-2.bqn - -u ⇐ •Import "/home/cph/bqn-test/test.bqn" - -AlienSort ← { 𝕩⊏˜⍋𝕨⊸⊐¨𝕩 } # Explicit -AlienSort2 ← ⍋∘(⊐˜¨⟜<)˜⊏⊢ # Tacit - -# Tests -u.UnitTest ("hlabydefgirkmnopqjstuvwxcz" AlienSort ⟨"perl", "python", "raku"⟩) ≡ ⟨"raku", "python", "perl"⟩ -u.UnitTest ("corldabtefghijkmnpqswuvxyz" AlienSort ⟨"the", "weekly", "challenge"⟩) ≡ ⟨"challenge", "the", "weekly"⟩ -u.UnitTest ("hlabydefgirkmnopqjstuvwxcz" AlienSort2 ⟨"perl", "python", "raku"⟩) ≡ ⟨"raku", "python", "perl"⟩ -u.UnitTest ("corldabtefghijkmnpqswuvxyz" AlienSort2 ⟨"the", "weekly", "challenge"⟩) ≡ ⟨"challenge", "the", "weekly"⟩ diff --git a/challenge-305/eric-cheung/python/ch-1.py b/challenge-305/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..39344cda93 --- /dev/null +++ b/challenge-305/eric-cheung/python/ch-1.py @@ -0,0 +1,16 @@ + +from sympy import isprime + +## arrBinary = [1, 0, 1] ## Example 1 +## arrBinary = [1, 1, 0] ## Example 2 +arrBinary = [1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1] ## Example 3 + +arrStrBinary = [str(nElem) for nElem in arrBinary] +arrOutput = [False] + +## for nIndx in range(1, len(arrStrBinary)): + ## arrOutput.append(isprime(int("".join(arrStrBinary[ : nIndx + 1]), 2))) + +arrOutput = arrOutput + [isprime(int("".join(arrStrBinary[ : nIndx + 1]), 2)) for nIndx in range(1, len(arrStrBinary))] + +print (arrOutput) diff --git a/challenge-305/eric-cheung/python/ch-2.py b/challenge-305/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..91237db3ca --- /dev/null +++ b/challenge-305/eric-cheung/python/ch-2.py @@ -0,0 +1,34 @@ + +def IsStrGreater(strInput_01, strInput_02): + global arrAlien + if strInput_01 == strInput_02: + return False + + nMinLen = min(len(strInput_01), len(strInput_02)) + + if strInput_01[:nMinLen] == strInput_02[:nMinLen]: + return len(strInput_01) > len(strInput_02) + + for nIndx in range(nMinLen): + if strInput_01[nIndx] == strInput_02[nIndx]: + continue + return arrAlien.index(strInput_01[nIndx]) > arrAlien.index(strInput_02[nIndx]) + +## Example 1 +## arrWords = ["perl", "python", "raku"] +## arrAlien = ["h", "l", "a", "b", "y", "d", "e", "f", "g", "i", "r", "k", "m", "n", "o", "p", "q", "j", "s", "t", "u", "v", "w", "x", "c", "z"] + +## Example 2 +arrWords = ["the", "weekly", "challenge"] +arrAlien = ["c", "o", "r", "l", "d", "a", "b", "t", "e", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "s", "w", "u", "v", "x", "y", "z"] + +for nRowLoop in range(len(arrWords) - 1): + for nColLoop in range(nRowLoop + 1, len(arrWords)): + if not IsStrGreater(arrWords[nRowLoop], arrWords[nColLoop]): + continue + + strTemp = arrWords[nRowLoop] + arrWords[nRowLoop] = arrWords[nColLoop] + arrWords[nColLoop] = strTemp + +print (arrWords) diff --git a/challenge-305/luca-ferrari/blog-1.txt b/challenge-305/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..a11f262a59 --- /dev/null +++ b/challenge-305/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1 diff --git a/challenge-305/luca-ferrari/blog-10.txt b/challenge-305/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..efa146d6f9 --- /dev/null +++ b/challenge-305/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2pljava diff --git a/challenge-305/luca-ferrari/blog-2.txt b/challenge-305/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..4b511d6565 --- /dev/null +++ b/challenge-305/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2 diff --git a/challenge-305/luca-ferrari/blog-3.txt b/challenge-305/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..ff43738b0f --- /dev/null +++ b/challenge-305/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1plperl diff --git a/challenge-305/luca-ferrari/blog-4.txt b/challenge-305/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..94da3eec7e --- /dev/null +++ b/challenge-305/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2plperl diff --git a/challenge-305/luca-ferrari/blog-5.txt b/challenge-305/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..f1f5420190 --- /dev/null +++ b/challenge-305/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1plpgsql diff --git a/challenge-305/luca-ferrari/blog-6.txt b/challenge-305/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..a3c426291c --- /dev/null +++ b/challenge-305/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2plpgsql diff --git a/challenge-305/luca-ferrari/blog-7.txt b/challenge-305/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..140cfd2d06 --- /dev/null +++ b/challenge-305/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1python diff --git a/challenge-305/luca-ferrari/blog-8.txt b/challenge-305/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..330e58a7c1 --- /dev/null +++ b/challenge-305/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2python diff --git a/challenge-305/luca-ferrari/blog-9.txt b/challenge-305/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..5c4a0e081f --- /dev/null +++ b/challenge-305/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1pljava diff --git a/challenge-305/luca-ferrari/pljava/pom.xml b/challenge-305/luca-ferrari/pljava/pom.xml new file mode 100644 index 0000000000..99c9085d7e --- /dev/null +++ b/challenge-305/luca-ferrari/pljava/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + PWC + + PWC306 + + + 1 + + + Perl Weekly Challenge 306 with package PWC306 + Implementation of the tasks in PL/Java for PWC 306 + + + US-ASCII + + + + + org.postgresql + pljava-api + 1.6.6 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 9 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + + + + true + + + + + + + pljava.ddr + + + true + + + + + + + + + + diff --git a/challenge-305/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-305/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..d2e48bcd52 --- /dev/null +++ b/challenge-305/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,81 @@ + + + +package PWC306; + +/** + * PL/Java implementation for PWC 306 + * Task 1 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC306-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC306-1.jar', 'PWC306', true ); + select sqlj.set_classpath( 'public', 'PWC306' ); + + select pwc306.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC306-1.jar', 'PWC306', true ); + +*/ + +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.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc306", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final Boolean[] task1_pljava( String[] bits ) throws SQLException { + logger.log( Level.INFO, "Entering pwc306.task1_pljava" ); + + List primes = new LinkedList(); + + for ( int i = 1; i <= bits.length; i++ ) { + int value = Integer.parseInt( String.join( "", Arrays.copyOfRange( bits, 0, i ) ), 2 ); + primes.add( is_prime( value ) ); + } + + Boolean result[] = new Boolean[ primes.size() ]; + for ( int i = 0; i < result.length; i++ ) + result[ i ] = primes.get( i ); + + + return result; + + } + + + private final static boolean is_prime( int n ) { + if ( n <= 1 ) + return false; + + for ( int i = 2; i < n; i++ ) + if ( n % i == 0 ) + return false; + + return true; + } +} diff --git a/challenge-305/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-305/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..bd365bdaba --- /dev/null +++ b/challenge-305/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,69 @@ + + + +package PWC306; + +/** + * PL/Java implementation for PWC 306 + * Task 2 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC306-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC306-1.jar', 'PWC306', true ); + select sqlj.set_classpath( 'public', 'PWC306' ); + + select pwc306.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC306-1.jar', 'PWC306', true ); + +*/ + +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.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc306", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String[] task2_pljava( String[] dictionary, String[] words ) throws SQLException { + logger.log( Level.INFO, "Entering pwc306.task2_pljava" ); + + List sorted = new LinkedList(); + + for ( int i = 0; i < dictionary.length; i++ ) { + for ( String w : words ) { + if ( dictionary[ i ].toLowerCase().equals( w.toLowerCase().substring( 0, 1 ) ) ) + sorted.add( w ); + } + } + + String[] result = new String[ sorted.size() ]; + for ( int i = 0; i < result.length; i++ ) + result[ i ] = sorted.get( i ); + + return result; + } +} diff --git a/challenge-305/luca-ferrari/plperl/ch-1.plperl b/challenge-305/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..ef52151fbe --- /dev/null +++ b/challenge-305/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,35 @@ +-- +-- Perl Weekly Challenge 306 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc306; + +CREATE OR REPLACE FUNCTION +pwc306.task1_plperl( bit[] ) +RETURNS SETOF bool +AS $CODE$ + + my ( $bits ) = @_; + + my $is_prime = sub { + my ( $number ) = @_; + return 0 if ( $number <= 1 ); + + for ( 2 .. $number - 1 ) { + return 0 if ( $number % $_ == 0 ); + } + + return 1; + }; + + for ( 0 .. $bits->@* - 1 ) { + my $int = oct( '0b' . join( '', $bits->@[ 0 .. $_ ] ) ); + return_next( $is_prime->( $int ) ); + } + + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-305/luca-ferrari/plperl/ch-2.plperl b/challenge-305/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..b684e6ed87 --- /dev/null +++ b/challenge-305/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,31 @@ +-- +-- Perl Weekly Challenge 306 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc306; + +CREATE OR REPLACE FUNCTION +pwc306.task2_plperl( char[], text[] ) +RETURNS SETOF text +AS $CODE$ + + my ( $dictionary, $words ) = @_; + + my $alien_dictionary = {}; + my $index = 0; + + $alien_dictionary->{ lc $_ } = $index++ for ( $dictionary->@* ); + + + my @sorted = + map { $_->[ 1 ] } + sort { $a->[ 0 ] <=> $b->[ 0 ] } + map { [ $alien_dictionary->{ lc( ( split //, $_ )[ 0 ] ) }, $_ ] } + $words->@*; + + return [ @sorted ]; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-305/luca-ferrari/plpgsql/ch-1.sql b/challenge-305/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..1739abe155 --- /dev/null +++ b/challenge-305/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,56 @@ +-- +-- Perl Weekly Challenge 306 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc306; + + +CREATE OR REPLACE FUNCTION is_prime( n int ) +RETURNS bool +AS $CODE$ +DECLARE + i int; +BEGIN + IF n <= 1 THEN + RETURN false; + END IF; + + FOR i IN 2 .. n - 1 LOOP + IF mod( n, i ) = 0 THEN + RETURN false; + END IF; + END LOOP; + + RETURN true; +END +$CODE$ +LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION +pwc306.task1_plpgsql( bits bit[] ) +RETURNS SETOF bool +AS $CODE$ +DECLARE + i int; + s text; +BEGIN + + FOR i in 1 .. array_length( bits, 1 ) LOOP + SELECT '0b' || string_agg( v::text, '' ) + INTO s + FROM unnest( bits[ 0 : i ] ) v; + + IF is_prime( s::int ) THEN + RETURN NEXT true; + ELSE + RETURN NEXT false; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-305/luca-ferrari/plpgsql/ch-2.sql b/challenge-305/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..c973eb42f6 --- /dev/null +++ b/challenge-305/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 306 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc306; + +CREATE OR REPLACE FUNCTION +pwc306.task2_plpgsql( dictionary char[], words text[] ) +RETURNS SETOF text +AS $CODE$ + SELECT pwc306.task2_plperl( dictionary, words ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-305/luca-ferrari/python/ch-1.py b/challenge-305/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..29b961e251 --- /dev/null +++ b/challenge-305/luca-ferrari/python/ch-1.py @@ -0,0 +1,37 @@ +#!python + +# +# Perl Weekly Challenge 306 +# Task 1 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + bits = list( map( int, args ) ) + primes = [] + + def is_prime( n ): + if n <= 1: + return False + + for i in range( 2, n - 1 ): + if n % i == 0: + return False + + return True + + for i in range( 0, len( bits ) ): + v = int( ''.join( list( map( str, bits[ 0 : i + 1 ] ) ) ), 2 ) + primes.append( is_prime( v ) ) + + return primes + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-305/luca-ferrari/python/ch-2.py b/challenge-305/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..5f98742484 --- /dev/null +++ b/challenge-305/luca-ferrari/python/ch-2.py @@ -0,0 +1,30 @@ +#!python + +# +# Perl Weekly Challenge 306 +# Task 2 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + dictionary = args[ 0 ].lower() + words = args[ 1 : ] + + sorted_words = [] + + for x in dictionary: + for w in words: + if w.lower()[ 0 ] == x: + sorted_words.append( w ) + + return sorted_words + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-305/luca-ferrari/raku/ch-1.raku b/challenge-305/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..565ee6744e --- /dev/null +++ b/challenge-305/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!raku + +# +# Perl Weekly Challenge 306 +# Task 1 +# +# See +# + +sub MAIN( *@bits where { @bits.elems == @bits.grep( * ~~ /<[01]>/ ).elems } ) { + + my @primes; + for 0 ..^ @bits.elems { + @primes.push: True and next if ( @bits[ 0 .. $_ ].join.parse-base( 10 ).is-prime ); + @primes.push: False; + } + + @primes.join( ', ' ).say; +} diff --git a/challenge-305/luca-ferrari/raku/ch-2.raku b/challenge-305/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..5fc73464f6 --- /dev/null +++ b/challenge-305/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 306 +# Task 2 +# +# See +# + +sub MAIN( Str $dictionary, *@words ) { + + my @sorted; + my @alien-dictionary = $dictionary.lc.comb( /<[a .. z]>/ ); + + @sorted = @words.sort( { @alien-dictionary.grep( * ~~ $^a.lc.comb[0], :k ).first <=> @alien-dictionary.grep( * ~~ $^b.lc.comb[0], :k ).first } ); + @sorted.join( ', ' ).say; +} diff --git a/challenge-305/perlboy1967/perl/ch-1.pl b/challenge-305/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..fe30952f58 --- /dev/null +++ b/challenge-305/perlboy1967/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 305 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Binary Prefix +Submitted by: Mohammad Sajid Anwar + +You are given a binary array. + +Write a script to return an array of booleans where the partial binary number +up to that point is prime. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0 qw(-no_srand); + +no warnings qw(experimental::signatures); + +use boolean; +use Math::Prime::XS qw(is_prime); + +sub binaryPrefix (@bits) { + my $b; + [map {$b .= $_; boolean is_prime oct "0b$b" } @bits]; +} + +is(binaryPrefix(1,0,1), + [false,true,true],'Example 1'); +is(binaryPrefix(1,1,0), + [false,true,false],'Example 2'); +is(binaryPrefix(1,1,1,1,0,1,0,0,0,0,1,0,1,0,0,1,0,0,0,1), + [0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],'Example 3'); + +done_testing; diff --git a/challenge-305/perlboy1967/perl/ch-2.pl b/challenge-305/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..ca3e7d4e52 --- /dev/null +++ b/challenge-305/perlboy1967/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 305 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Alien Dictionary +Submitted by: Mohammad Sajid Anwar + +You are given a list of words and alien dictionary character order. + +Write a script to sort lexicographically the given list of words based on the alien +dictionary characters. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0 qw(-no_srand); + +no warnings qw(experimental::signatures); + +sub alienDictionary :prototype(\@\@) ($arWords,$arAlien) { + my $alien = join('',@$arAlien); + [ + sort { + eval "\$a =~ tr/abcdefghijklmnopqrstuvwxyz/$alien/r" + cmp + eval "\$b =~ tr/abcdefghijklmnopqrstuvwxyz/$alien/r"; + } @$arWords + ] +} + +is(alienDictionary(@{[qw(perl python raku)]}, + @{[qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/]}), + [qw(raku python perl)],'Example 1'); +is(alienDictionary(@{[qw(the weekly challenge)]}, + @{[qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/]}), + [qw(challenge the weekly)],'Example 2'); + +done_testing; diff --git a/challenge-305/perlboy1967/perl/ch1.pl b/challenge-305/perlboy1967/perl/ch1.pl deleted file mode 100755 index fe30952f58..0000000000 --- a/challenge-305/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 305 -L - -Author: Niels 'PerlBoy' van Dijke - -Task 1: Binary Prefix -Submitted by: Mohammad Sajid Anwar - -You are given a binary array. - -Write a script to return an array of booleans where the partial binary number -up to that point is prime. - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); - -use Test2::V0 qw(-no_srand); - -no warnings qw(experimental::signatures); - -use boolean; -use Math::Prime::XS qw(is_prime); - -sub binaryPrefix (@bits) { - my $b; - [map {$b .= $_; boolean is_prime oct "0b$b" } @bits]; -} - -is(binaryPrefix(1,0,1), - [false,true,true],'Example 1'); -is(binaryPrefix(1,1,0), - [false,true,false],'Example 2'); -is(binaryPrefix(1,1,1,1,0,1,0,0,0,0,1,0,1,0,0,1,0,0,0,1), - [0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],'Example 3'); - -done_testing; diff --git a/challenge-305/perlboy1967/perl/ch2.pl b/challenge-305/perlboy1967/perl/ch2.pl deleted file mode 100755 index ca3e7d4e52..0000000000 --- a/challenge-305/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 305 -L - -Author: Niels 'PerlBoy' van Dijke - -Task 2: Alien Dictionary -Submitted by: Mohammad Sajid Anwar - -You are given a list of words and alien dictionary character order. - -Write a script to sort lexicographically the given list of words based on the alien -dictionary characters. - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); - -use Test2::V0 qw(-no_srand); - -no warnings qw(experimental::signatures); - -sub alienDictionary :prototype(\@\@) ($arWords,$arAlien) { - my $alien = join('',@$arAlien); - [ - sort { - eval "\$a =~ tr/abcdefghijklmnopqrstuvwxyz/$alien/r" - cmp - eval "\$b =~ tr/abcdefghijklmnopqrstuvwxyz/$alien/r"; - } @$arWords - ] -} - -is(alienDictionary(@{[qw(perl python raku)]}, - @{[qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/]}), - [qw(raku python perl)],'Example 1'); -is(alienDictionary(@{[qw(the weekly challenge)]}, - @{[qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/]}), - [qw(challenge the weekly)],'Example 2'); - -done_testing; diff --git a/challenge-305/richard-park/apl/AlienDictionary.aplf b/challenge-305/richard-park/apl/AlienDictionary.aplf new file mode 100644 index 0000000000..aaff84bd23 --- /dev/null +++ b/challenge-305/richard-park/apl/AlienDictionary.aplf @@ -0,0 +1,7 @@ +AlienDictionary←{ +⍝ ⍵: List of strings +⍝ ⍺: Character dictionary :: simple character vector +⍝ ←: ⍵ sorted lexicographically according to ⍺ + ⍵[⍺⍋↑⍵] + } + diff --git a/challenge-305/richard-park/apl/BinaryPrefix.aplf b/challenge-305/richard-park/apl/BinaryPrefix.aplf new file mode 100644 index 0000000000..c4cb1be3fe --- /dev/null +++ b/challenge-305/richard-park/apl/BinaryPrefix.aplf @@ -0,0 +1,6 @@ + BinaryPrefix←{ +⍝ ⍵: Boolean vector +⍝ ←: Boolean vector in which 1s mark the ends of prefixes that are +⍝ prime numbers in decimal + 2=+⌿0=(⍳⌈/n)∘.|n←2⊥i⌽(i←⍳≢⍵)(↑⍤0 1)⍵ + } diff --git a/challenge-305/richard-park/apl/W305_APL.ipynb b/challenge-305/richard-park/apl/W305_APL.ipynb new file mode 100644 index 0000000000..aa78155342 --- /dev/null +++ b/challenge-305/richard-park/apl/W305_APL.ipynb @@ -0,0 +1,450 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "84744954-b16a-4923-81d4-07be002b5f09", + "metadata": {}, + "source": [ + "# Perl Weekly Challenge 305\n", + "These are solutions to [Perl Weekly Challenge 305](https://theweeklychallenge.org/blog/perl-weekly-challenge-305/) in [Dyalog APL](https://www.dyalog.com/)." + ] + }, + { + "cell_type": "markdown", + "id": "2829f2d3-9515-4b16-ace4-682f6aa1f044", + "metadata": {}, + "source": [ + "## Task 1: Binary Prefix\n", + "Take a Boolean vector as input and return a Boolean vector where a 1 indicates that the partial binary number up to that element is a prime number.\n", + "\n", + "For example, `BinaryPrefix 1 0 1` returns `0 1 1` because:\n", + "- $1$ is not prime\n", + "- $2=b_2(1 0)$ is prime\n", + "- $5=b_2(1 0 1)$ is prime" + ] + }, + { + "cell_type": "markdown", + "id": "f2d288c4-392a-49db-8028-9779676bb709", + "metadata": {}, + "source": [ + "### Creating prefixes\n", + "There are several valid approaches. The simplest is a **catenate-scan** `,\\⍵`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e23962d8-3008-4cab-9f58-ae94e726515e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Was ON\n",
+       "
" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "text/html": [ + "
┌─┬───┬─────┬───────┬─────────┬───────────┬─────────────┐\n",
+       "│1│1 0│1 0 1│1 0 1 1│1 0 1 1 0│1 0 1 1 0 1│1 0 1 1 0 1 1│\n",
+       "└─┴───┴─────┴───────┴─────────┴───────────┴─────────────┘\n",
+       "
" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "]box on\n", + ",\\1 0 1 1 0 1 1" + ] + }, + { + "cell_type": "markdown", + "id": "d99ebccb-5a07-46a7-a887-bd4ef3d22a01", + "metadata": {}, + "source": [ + "We can then convert each number with the base conversion primitive." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "6345a2a6-6168-485a-b0d6-b71bd3b35444", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
1 0 1  1  0  1  1\n",
+       "1 2 5 11 22 45 91\n",
+       "
" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b←1 0 1 1 0 1 1\n", + "↑(b)(2⊥¨,\\b)" + ] + }, + { + "cell_type": "markdown", + "id": "43c244c5-7a53-4419-88ca-9cc03e1f6ace", + "metadata": {}, + "source": [ + "It can be more efficient to build a flat array that works directly with primitive functions in APL, rather than looping using the each `F¨` operator. In the following expression, we build a flat Boolean matrix of prefixes using [the rank operator](https://course.dyalog.com/cells-and-axes/#the-rank-operator). The base conversion primitive function can then work on the entire array at once rather than having to loop over individual prefixes. This is significantly faster than `2⊥¨,\\b`." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "a10a5ebb-e7b6-437a-8927-6fd555118141", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
1 2 5 11 22 45 91\n",
+       "
" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2⊥i⌽(i←⍳≢b)(↑⍤0 1)b" + ] + }, + { + "cell_type": "markdown", + "id": "72d3032f-7bbd-4d9f-8e89-f5e393c44d3a", + "metadata": {}, + "source": [ + "### Checking Primes\n", + "A direct approach is to use the \"unique divisors\" idiom." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "43d605b8-bd1e-4589-8bfc-bae3d7e5278b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
┌─┬───┬───┬────┬─────────┬─────────────┬─────────┐\n",
+       "│1│1 2│1 5│1 11│1 2 11 22│1 3 5 9 15 45│1 7 13 91│\n",
+       "└─┴───┴───┴────┴─────────┴─────────────┴─────────┘\n",
+       "
" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(∪⊢∨⍳)¨1 2 5 11 22 45 91" + ] + }, + { + "cell_type": "markdown", + "id": "b878cdaa-94e0-422c-b740-266c301634a3", + "metadata": {}, + "source": [ + "We can then count the unique divisors and check which have exactly two, which is the definition of being a prime number." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "a035c3f8-7b4b-4dc8-bfdc-791774daa4b0", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
0 1 1 1 0 0 0\n",
+       "
" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2=(≢∘∪⊢∨⍳)¨1 2 5 11 22 45 91" + ] + }, + { + "cell_type": "markdown", + "id": "24f817ed-67ea-4f48-aa64-f206228ba8f9", + "metadata": {}, + "source": [ + "The `(∪⊢∨⍳)` train is cute and somewhat says exactly what we want: \"unique divisors of ⍵\". However, it is quite inefficient to find all divisors between every number and the list of integers up to that number.\n", + "\n", + "Instead, let's find where all integers up to the largest in our list are evenly divided with the modulo function." + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "2e91cf5a-a8fd-4850-acfb-a5eb0f20ac7b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
0 1 1 1 0 0 0\n",
+       "
" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{2=+⌿0=(⍳⌈/⍵)∘.|⍵}1 2 5 11 22 45 91" + ] + }, + { + "cell_type": "markdown", + "id": "b453f539-f181-4ae7-a238-2bcb8ca4c159", + "metadata": {}, + "source": [ + "A yet more efficient prime number checker is found in the `pco` function in the [dfns workspace](https://dfns.dyalog.com), although it is a simple lookup for small primes." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "c8da54f6-e8f7-409c-a823-01aab97453f0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
0 1 1 1 0 0 0\n",
+       "
" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'pco'⎕CY'dfns'\n", + "1 pco 1 2 5 11 22 45 91" + ] + }, + { + "cell_type": "markdown", + "id": "45f2d65f-06a4-47b0-b41f-634f8d33be6d", + "metadata": {}, + "source": [ + "Let's put this all together and try the test cases from the website." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "44fff76f-e7c2-4bbf-b3fc-57ec08e0b2a5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
0 1 1\n",
+       "
" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "text/html": [ + "
0 1 0\n",
+       "
" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "text/html": [ + "
0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1\n",
+       "
" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "BinaryPrefix ← {2=+⌿0=(⍳⌈/n)∘.|n←2⊥i⌽(i←⍳≢⍵)(↑⍤0 1)⍵}\n", + "BinaryPrefix 1 0 1\n", + "BinaryPrefix 1 1 0\n", + "BinaryPrefix 1 1 1 1 0 1 0 0 0 0 1 0 1 0 0 1 0 0 0 1" + ] + }, + { + "cell_type": "markdown", + "id": "d9e4e6c1-7f4a-4615-bd72-abbd90657d3c", + "metadata": {}, + "source": [ + "## Task 2: Alien Dictionary\n", + "You are given a list of words and alien dictionary character order.\n", + "\n", + "Write a script to sort lexicographically the given list of words based on the alien dictionary characters." + ] + }, + { + "cell_type": "markdown", + "id": "4451f901-1fe2-45de-b7ee-c92b6706f005", + "metadata": {}, + "source": [ + "First, let's get our inputs. The Raku language appears to have the `qw` function that creates a list of characters. In APL, that is the default format for character data, so we'll just paste the input from the website and remove the spaces." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "4f488e2d-d3ef-4d5e-a9bf-69c6e56ae95d", + "metadata": {}, + "outputs": [], + "source": [ + "alien ← 'h l a b y d e f g i r k m n o p q j s t u v w x c z'~' '" + ] + }, + { + "cell_type": "markdown", + "id": "e4d46804-3e31-494a-8e88-8fe1bd205678", + "metadata": {}, + "source": [ + "For convenience, we can also take the raw text for the list of strings, swap round with square parentheses, and process it with `⎕JSON`." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "edcc0d48-ed15-4d22-95c7-bd0a84150e6c", + "metadata": {}, + "outputs": [], + "source": [ + "words ← 0⎕JSON'[\"perl\", \"python\", \"raku\"]'" + ] + }, + { + "cell_type": "markdown", + "id": "906c60f6-030b-48f9-8736-2a24a8ea43bc", + "metadata": {}, + "source": [ + "As for our solution, this is simply one of those unusual times when you actually need dyadic grade.\n", + "\n", + "Dyadic grade takes a simple character vector dictionary left argument `⍺` and a simple character array right argument `⍵` and lexicographically sorts major cells in `⍵` according to the order of elements in `⍺`, leaving those not found in `⍺` in their original order.\n", + "\n", + "The only thing to note is that dyadic grade only works on simple character data, so we must mix our input list of words to make a simple character matrix. However, we can use the grade on the original input list of strings." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "c846acbe-8450-420f-be85-8c2bff2ff288", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
┌────┬──────┬────┐\n",
+       "│raku│python│perl│\n",
+       "└────┴──────┴────┘\n",
+       "
" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alien {⍵[⍺⍋↑⍵]} words" + ] + }, + { + "cell_type": "markdown", + "id": "7db8a53f-4c1b-40d2-82bb-0c7d2b918133", + "metadata": {}, + "source": [ + "And finally, the second example inputs." + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "6404794f-3b51-4ff1-b62b-3d6e9e669b99", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
┌─────────┬───┬──────┐\n",
+       "│challenge│the│weekly│\n",
+       "└─────────┴───┴──────┘\n",
+       "
" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "alien ← 'c o r l d a b t e f g h i j k m n p q s w u v x y z'~' '\n", + "words ← 0⎕JSON'[\"the\", \"weekly\", \"challenge\"]'\n", + "alien {⍵[⍺⍋↑⍵]} words" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Dyalog APL", + "language": "apl", + "name": "dyalog_apl" + }, + "language_info": { + "file_extension": ".apl", + "mimetype": "text/apl", + "name": "APL" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/challenge-305/zapwai/ch-1.pl b/challenge-305/zapwai/ch-1.pl deleted file mode 100644 index af6161d101..0000000000 --- a/challenge-305/zapwai/ch-1.pl +++ /dev/null @@ -1,25 +0,0 @@ -use v5.38; - -sub bin_to_dec(@bin) { - @bin = reverse @bin; - my $sum = 0; - for my $i (0 .. $#bin) { - $sum += $bin[$i]*2**$i - } - return $sum; -} - -sub proc(@bin) { - say "Input: \@bin = @bin"; - my @t; - for my $i (0 .. $#bin) { - push @t, (bin_to_dec(@bin[0..$i])%2 == 0) ? "true" : "false"; - } - print "Output: "; say "@t"; -} - -my @bin = (1,0,1); -proc(@bin); - -@bin = (1,1,0); -proc(@bin); diff --git a/challenge-305/zapwai/ch-2.pl b/challenge-305/zapwai/ch-2.pl deleted file mode 100644 index a8a835bda0..0000000000 --- a/challenge-305/zapwai/ch-2.pl +++ /dev/null @@ -1,45 +0,0 @@ -use v5.38; -sub out_of_order($dict, $a, $b) { - my @la = split '', $a; - my @lb = split '', $b; - my $N = (length $a < length $b) ? length $a : length $b; - for my $i (0 .. $N - 1) { - next if ($lb[$i] eq $la[$i]); - my $ib = index $dict, $lb[$i]; - my $ia = index $dict, $la[$i]; - return ($ib < $ia) ? 1 : 0; - } -} - -sub alien($dict, $w) { - my @words = @$w; - my @list = @words; - my $n = 0; - do { - $n = 0; - for my $i (0 .. $#list - 1) { - if (out_of_order($dict, $list[$i], $list[$i+1])) { - $n++; - my $temp = $list[$i]; - $list[$i] = $list[$i + 1]; - $list[$i + 1] = $temp; - last; - } - } - } while ($n != 0); - return "@list"; -} - -sub proc($w, $a) { - say "Input: words: @$w"; - say "\tdict: $a"; - say "Output: ", alien($a, $w); -} - -my @words = ("perl", "python", "raku"); -my @alien = qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/; -proc(\@words, join '', @alien); - -@words = ("the", "weekly", "challenge"); -@alien = qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/; -proc(\@words, join '', @alien); diff --git a/challenge-305/zapwai/perl/ch-1.pl b/challenge-305/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..af6161d101 --- /dev/null +++ b/challenge-305/zapwai/perl/ch-1.pl @@ -0,0 +1,25 @@ +use v5.38; + +sub bin_to_dec(@bin) { + @bin = reverse @bin; + my $sum = 0; + for my $i (0 .. $#bin) { + $sum += $bin[$i]*2**$i + } + return $sum; +} + +sub proc(@bin) { + say "Input: \@bin = @bin"; + my @t; + for my $i (0 .. $#bin) { + push @t, (bin_to_dec(@bin[0..$i])%2 == 0) ? "true" : "false"; + } + print "Output: "; say "@t"; +} + +my @bin = (1,0,1); +proc(@bin); + +@bin = (1,1,0); +proc(@bin); diff --git a/challenge-305/zapwai/perl/ch-2.pl b/challenge-305/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..a8a835bda0 --- /dev/null +++ b/challenge-305/zapwai/perl/ch-2.pl @@ -0,0 +1,45 @@ +use v5.38; +sub out_of_order($dict, $a, $b) { + my @la = split '', $a; + my @lb = split '', $b; + my $N = (length $a < length $b) ? length $a : length $b; + for my $i (0 .. $N - 1) { + next if ($lb[$i] eq $la[$i]); + my $ib = index $dict, $lb[$i]; + my $ia = index $dict, $la[$i]; + return ($ib < $ia) ? 1 : 0; + } +} + +sub alien($dict, $w) { + my @words = @$w; + my @list = @words; + my $n = 0; + do { + $n = 0; + for my $i (0 .. $#list - 1) { + if (out_of_order($dict, $list[$i], $list[$i+1])) { + $n++; + my $temp = $list[$i]; + $list[$i] = $list[$i + 1]; + $list[$i + 1] = $temp; + last; + } + } + } while ($n != 0); + return "@list"; +} + +sub proc($w, $a) { + say "Input: words: @$w"; + say "\tdict: $a"; + say "Output: ", alien($a, $w); +} + +my @words = ("perl", "python", "raku"); +my @alien = qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/; +proc(\@words, join '', @alien); + +@words = ("the", "weekly", "challenge"); +@alien = qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/; +proc(\@words, join '', @alien); diff --git a/challenge-306/luca-ferrari/blog-1.txt b/challenge-306/luca-ferrari/blog-1.txt deleted file mode 100644 index a11f262a59..0000000000 --- a/challenge-306/luca-ferrari/blog-1.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1 diff --git a/challenge-306/luca-ferrari/blog-10.txt b/challenge-306/luca-ferrari/blog-10.txt deleted file mode 100644 index efa146d6f9..0000000000 --- a/challenge-306/luca-ferrari/blog-10.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2pljava diff --git a/challenge-306/luca-ferrari/blog-2.txt b/challenge-306/luca-ferrari/blog-2.txt deleted file mode 100644 index 4b511d6565..0000000000 --- a/challenge-306/luca-ferrari/blog-2.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2 diff --git a/challenge-306/luca-ferrari/blog-3.txt b/challenge-306/luca-ferrari/blog-3.txt deleted file mode 100644 index ff43738b0f..0000000000 --- a/challenge-306/luca-ferrari/blog-3.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1plperl diff --git a/challenge-306/luca-ferrari/blog-4.txt b/challenge-306/luca-ferrari/blog-4.txt deleted file mode 100644 index 94da3eec7e..0000000000 --- a/challenge-306/luca-ferrari/blog-4.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2plperl diff --git a/challenge-306/luca-ferrari/blog-5.txt b/challenge-306/luca-ferrari/blog-5.txt deleted file mode 100644 index f1f5420190..0000000000 --- a/challenge-306/luca-ferrari/blog-5.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1plpgsql diff --git a/challenge-306/luca-ferrari/blog-6.txt b/challenge-306/luca-ferrari/blog-6.txt deleted file mode 100644 index a3c426291c..0000000000 --- a/challenge-306/luca-ferrari/blog-6.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2plpgsql diff --git a/challenge-306/luca-ferrari/blog-7.txt b/challenge-306/luca-ferrari/blog-7.txt deleted file mode 100644 index 140cfd2d06..0000000000 --- a/challenge-306/luca-ferrari/blog-7.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1python diff --git a/challenge-306/luca-ferrari/blog-8.txt b/challenge-306/luca-ferrari/blog-8.txt deleted file mode 100644 index 330e58a7c1..0000000000 --- a/challenge-306/luca-ferrari/blog-8.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task2python diff --git a/challenge-306/luca-ferrari/blog-9.txt b/challenge-306/luca-ferrari/blog-9.txt deleted file mode 100644 index 5c4a0e081f..0000000000 --- a/challenge-306/luca-ferrari/blog-9.txt +++ /dev/null @@ -1 +0,0 @@ -https://fluca1978.github.io/2025/01/20/PerlWeeklyChallenge306.html#task1pljava diff --git a/challenge-306/luca-ferrari/pljava/pom.xml b/challenge-306/luca-ferrari/pljava/pom.xml deleted file mode 100644 index 99c9085d7e..0000000000 --- a/challenge-306/luca-ferrari/pljava/pom.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - 4.0.0 - - PWC - - PWC306 - - - 1 - - - Perl Weekly Challenge 306 with package PWC306 - Implementation of the tasks in PL/Java for PWC 306 - - - US-ASCII - - - - - org.postgresql - pljava-api - 1.6.6 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - - 9 - - - - org.apache.maven.plugins - maven-jar-plugin - 2.6 - - - - - - true - - - - - - - pljava.ddr - - - true - - - - - - - - - - diff --git a/challenge-306/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-306/luca-ferrari/pljava/src/main/java/Task1.java deleted file mode 100644 index d2e48bcd52..0000000000 --- a/challenge-306/luca-ferrari/pljava/src/main/java/Task1.java +++ /dev/null @@ -1,81 +0,0 @@ - - - -package PWC306; - -/** - * PL/Java implementation for PWC 306 - * Task 1 - * See - * - * - * To compile on the local machine: - - $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set - $ mvn clean build - $ scp target/PWC306-1.jar luca@rachel:/tmp - - - * To install into PostgreSQL execute: - - select sqlj.install_jar( 'file:///tmp/PWC306-1.jar', 'PWC306', true ); - select sqlj.set_classpath( 'public', 'PWC306' ); - - select pwc306.task2_pljava(); - - and then to redeploy: - - select sqlj.replace_jar( 'file:///tmp/PWC306-1.jar', 'PWC306', true ); - -*/ - -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.*; -import java.util.stream.*; -import java.sql.SQLException; -import java.util.logging.*; -import java.sql.ResultSet; -import java.sql.Date; - -public class Task1 { - - private final static Logger logger = Logger.getAnonymousLogger(); - - @Function( schema = "pwc306", - onNullInput = RETURNS_NULL, - effects = IMMUTABLE ) - public static final Boolean[] task1_pljava( String[] bits ) throws SQLException { - logger.log( Level.INFO, "Entering pwc306.task1_pljava" ); - - List primes = new LinkedList(); - - for ( int i = 1; i <= bits.length; i++ ) { - int value = Integer.parseInt( String.join( "", Arrays.copyOfRange( bits, 0, i ) ), 2 ); - primes.add( is_prime( value ) ); - } - - Boolean result[] = new Boolean[ primes.size() ]; - for ( int i = 0; i < result.length; i++ ) - result[ i ] = primes.get( i ); - - - return result; - - } - - - private final static boolean is_prime( int n ) { - if ( n <= 1 ) - return false; - - for ( int i = 2; i < n; i++ ) - if ( n % i == 0 ) - return false; - - return true; - } -} diff --git a/challenge-306/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-306/luca-ferrari/pljava/src/main/java/Task2.java deleted file mode 100644 index bd365bdaba..0000000000 --- a/challenge-306/luca-ferrari/pljava/src/main/java/Task2.java +++ /dev/null @@ -1,69 +0,0 @@ - - - -package PWC306; - -/** - * PL/Java implementation for PWC 306 - * Task 2 - * See - * - * - * To compile on the local machine: - - $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set - $ mvn clean build - $ scp target/PWC306-1.jar luca@rachel:/tmp - - - * To install into PostgreSQL execute: - - select sqlj.install_jar( 'file:///tmp/PWC306-1.jar', 'PWC306', true ); - select sqlj.set_classpath( 'public', 'PWC306' ); - - select pwc306.task2_pljava(); - - and then to redeploy: - - select sqlj.replace_jar( 'file:///tmp/PWC306-1.jar', 'PWC306', true ); - -*/ - -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.*; -import java.util.stream.*; -import java.sql.SQLException; -import java.util.logging.*; -import java.sql.ResultSet; -import java.sql.Date; - -public class Task2 { - - private final static Logger logger = Logger.getAnonymousLogger(); - - @Function( schema = "pwc306", - onNullInput = RETURNS_NULL, - effects = IMMUTABLE ) - public static final String[] task2_pljava( String[] dictionary, String[] words ) throws SQLException { - logger.log( Level.INFO, "Entering pwc306.task2_pljava" ); - - List sorted = new LinkedList(); - - for ( int i = 0; i < dictionary.length; i++ ) { - for ( String w : words ) { - if ( dictionary[ i ].toLowerCase().equals( w.toLowerCase().substring( 0, 1 ) ) ) - sorted.add( w ); - } - } - - String[] result = new String[ sorted.size() ]; - for ( int i = 0; i < result.length; i++ ) - result[ i ] = sorted.get( i ); - - return result; - } -} diff --git a/challenge-306/luca-ferrari/plperl/ch-1.plperl b/challenge-306/luca-ferrari/plperl/ch-1.plperl deleted file mode 100644 index ef52151fbe..0000000000 --- a/challenge-306/luca-ferrari/plperl/ch-1.plperl +++ /dev/null @@ -1,35 +0,0 @@ --- --- Perl Weekly Challenge 306 --- Task 1 --- See --- - -CREATE SCHEMA IF NOT EXISTS pwc306; - -CREATE OR REPLACE FUNCTION -pwc306.task1_plperl( bit[] ) -RETURNS SETOF bool -AS $CODE$ - - my ( $bits ) = @_; - - my $is_prime = sub { - my ( $number ) = @_; - return 0 if ( $number <= 1 ); - - for ( 2 .. $n