diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-23 16:34:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-23 16:34:40 +0100 |
| commit | 705fa6a401e9ecd089fb55e0cb45df905bbfd1bf (patch) | |
| tree | 605445722d80e3486bac114ac184c4bec15baecc /challenge-266/luca-ferrari/pljava/src/main | |
| parent | 59d11166c24beb75c69862f9916592a4fadfb8ce (diff) | |
| parent | 5dba324368d526b878978f60d55ae1db3b795804 (diff) | |
| download | perlweeklychallenge-club-705fa6a401e9ecd089fb55e0cb45df905bbfd1bf.tar.gz perlweeklychallenge-club-705fa6a401e9ecd089fb55e0cb45df905bbfd1bf.tar.bz2 perlweeklychallenge-club-705fa6a401e9ecd089fb55e0cb45df905bbfd1bf.zip | |
Merge pull request #9974 from fluca1978/PWC266
PWC 266
Diffstat (limited to 'challenge-266/luca-ferrari/pljava/src/main')
| -rw-r--r-- | challenge-266/luca-ferrari/pljava/src/main/java/Task1.java | 99 | ||||
| -rw-r--r-- | challenge-266/luca-ferrari/pljava/src/main/java/Task2.java | 70 |
2 files changed, 169 insertions, 0 deletions
diff --git a/challenge-266/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-266/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..895641d052 --- /dev/null +++ b/challenge-266/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,99 @@ + + + +package PWC266; + +/** + * PL/Java implementation for PWC 266 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-266> + * + * + * 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/PWC266-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC266-1.jar', 'PWC266', true ); + select sqlj.set_classpath( 'public', 'PWC266' ); + + select pwc266.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC266-1.jar', 'PWC266', 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 = "pwc266", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String[] task1_pljava( String left, String right ) throws SQLException { + logger.log( Level.INFO, "Entering pwc266.task1_pljava" ); + + Map<String, Integer> bag_left = new HashMap<String, Integer>(); + Map<String, Integer> bag_right = new HashMap<String, Integer>(); + + classify( bag_left, left ); + classify( bag_right, right ); + + + List<String> words = deduplicate( bag_left, bag_right ); + String returning[] = new String[ words.size() ]; + int i = 0; + for ( String word : words ) + returning[ i++ ] = word; + + return returning; + } + + + private static final List<String> deduplicate( Map<String, Integer> bag_left, + Map<String, Integer> bag_right ) { + + List<String> words = new LinkedList<String>(); + + for ( String word : bag_left.keySet() ) { + if ( bag_left.get( word ) == 1 && ! bag_right.containsKey( word ) ) + words.add( word ); + } + + for ( String word : bag_right.keySet() ) { + if ( bag_right.get( word ) == 1 && ! bag_left.containsKey( word ) ) + words.add( word ); + } + + return words; + } + + private static final void classify( Map<String, Integer> bag, String sentence ) { + for ( String word : sentence.split( "\\W+" ) ) { + word = word.toLowerCase(); + int count = 1; + if ( bag.containsKey( word ) ) + count += bag.get( word ); + + bag.put( word, count ); + } + } +} diff --git a/challenge-266/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-266/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..14f94425dd --- /dev/null +++ b/challenge-266/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,70 @@ + + + +package PWC266; + +/** + * PL/Java implementation for PWC 266 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-266> + * + * + * 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/PWC266-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC266-1.jar', 'PWC266', true ); + select sqlj.set_classpath( 'public', 'PWC266' ); + + select pwc266.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC266-1.jar', 'PWC266', 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 = "pwc266", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task2_pljava( int dim, int[] matrix ) throws SQLException { + logger.log( Level.INFO, "Entering pwc266.task2_pljava" ); + + for ( int row = 0; row < dim; row++ ) { + for ( int col = 0; col < dim; col++ ) { + + if ( row == col || col == dim - row - 1 ) { + if ( matrix[ row * dim + col ] == 0 ) + return false; + } + else if ( matrix[ row * dim + col ] != 0 ) + return false; + + } + } + + return true; + + } +} |
