aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-194/luca-ferrari/postgresql/ch-2.plperl26
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-194/luca-ferrari/postgresql/ch-2.plperl b/challenge-194/luca-ferrari/postgresql/ch-2.plperl
new file mode 100644
index 0000000000..492213c917
--- /dev/null
+++ b/challenge-194/luca-ferrari/postgresql/ch-2.plperl
@@ -0,0 +1,26 @@
+-- Perl Weekly Challenge 194
+-- Task 2
+
+CREATE SCHEMA IF NOT EXISTS pwc194;
+
+CREATE OR REPLACE FUNCTION
+pwc194.task2_plperl( text )
+RETURNS int
+AS $CODE$
+ my ( $what ) = @_;
+
+ my %counter;
+ my ( $max, $min ) = ( 0, 0 );
+
+ for ( split '', $what ) {
+ $counter{ $_ }++;
+ $min = $counter{ $_ } if ( ! $min || $min > $counter{ $_ } );
+ $max = $counter{ $_ } if ( ! $max || $max < $counter{ $_ } );
+ }
+
+ return 0 if ( $max - $min != 1 );
+ return 0 if ( grep( { $counter{ $_ } == $max } keys %counter ) != 1 );
+ return 1;
+
+$CODE$
+LANGUAGE plperl;