diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-12 13:46:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-12 13:46:13 +0100 |
| commit | b341bdd5b98752ba83f46cf3c336258862295575 (patch) | |
| tree | 7ba71ad0c57453c5194a04681dc4d2ad80fddd29 /challenge-325/luca-ferrari/pljava/src/main/java | |
| parent | dbcca4b9906de55925c588a3d16889aa3f697a8f (diff) | |
| parent | d683f6d8d28d1a5ccb05bc47053cb0067151278a (diff) | |
| download | perlweeklychallenge-club-b341bdd5b98752ba83f46cf3c336258862295575.tar.gz perlweeklychallenge-club-b341bdd5b98752ba83f46cf3c336258862295575.tar.bz2 perlweeklychallenge-club-b341bdd5b98752ba83f46cf3c336258862295575.zip | |
Merge pull request #12170 from fluca1978/PWC325
PWC 325
Diffstat (limited to 'challenge-325/luca-ferrari/pljava/src/main/java')
| -rw-r--r-- | challenge-325/luca-ferrari/pljava/src/main/java/Task1.java | 71 | ||||
| -rw-r--r-- | challenge-325/luca-ferrari/pljava/src/main/java/Task2.java | 74 |
2 files changed, 145 insertions, 0 deletions
diff --git a/challenge-325/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-325/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..2cb8924dda --- /dev/null +++ b/challenge-325/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,71 @@ + + + +package PWC325; + +/** + * PL/Java implementation for PWC 325 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> + * + * + * 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/PWC325-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC325-1.jar', 'PWC325', true ); + select sqlj.set_classpath( 'public', 'PWC325' ); + + select pwc325.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC325-1.jar', 'PWC325', 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 = "pwc325", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( String bits ) throws SQLException { + logger.log( Level.INFO, "Entering pwc325.task1_pljava" ); + + int max_ones = 0; + int current_max = 0; + + for ( String d : bits.split( "" ) ) { + if ( d.equals( "1" ) ) + current_max++; + else { + if ( current_max > max_ones ) + max_ones = current_max; + + current_max = 0; + + } + } + + return max_ones; + } +} diff --git a/challenge-325/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-325/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..aa4fdedeee --- /dev/null +++ b/challenge-325/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,74 @@ + + + +package PWC325; + +/** + * PL/Java implementation for PWC 325 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> + * + * + * 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/PWC325-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC325-1.jar', 'PWC325', true ); + select sqlj.set_classpath( 'public', 'PWC325' ); + + select pwc325.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC325-1.jar', 'PWC325', 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 = "pwc325", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int[] task2_pljava( int[] prices ) throws SQLException { + logger.log( Level.INFO, "Entering pwc325.task2_pljava" ); + + List<Integer> new_prices = new LinkedList<Integer>(); + + for ( int i = 0; i < prices.length; i++ ) { + int current = prices[ i ]; + + for ( int j = i + 1; j < prices.length; j++ ) + if ( prices[ j ] < prices[ i ] ) { + current -= prices[ j ]; + break; + } + + new_prices.add( current ); + } + + int result[] = new int[ new_prices.size() ]; + for ( int i = 0; i < new_prices.size(); i++ ) + result[ i ] = new_prices.get( i ); + + return result; + } +} |
