diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-06 16:39:06 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-06 16:39:06 +0000 |
| commit | 1c3aa995c17a9b68ebe33d3ccd3e3602f9d7fb0d (patch) | |
| tree | 0a762a3f7b141f6a42a91c515197a9c55fee71a2 | |
| parent | 0715a1ec39bb0ba5e9bb2f3d58122f49d6b4a521 (diff) | |
| parent | 779550e17952192c0f762f23e8a5d9386da3e5fb (diff) | |
| download | perlweeklychallenge-club-1c3aa995c17a9b68ebe33d3ccd3e3602f9d7fb0d.tar.gz perlweeklychallenge-club-1c3aa995c17a9b68ebe33d3ccd3e3602f9d7fb0d.tar.bz2 perlweeklychallenge-club-1c3aa995c17a9b68ebe33d3ccd3e3602f9d7fb0d.zip | |
Merge pull request #7217 from fluca1978/PWC194
Pwc194
| -rw-r--r-- | challenge-194/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/postgresql/ch-1.plperl | 31 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/postgresql/ch-1.sql | 39 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/postgresql/ch-2.plperl | 26 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/postgresql/ch-2.sql | 46 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/raku/ch-1.p6 | 14 | ||||
| -rw-r--r-- | challenge-194/luca-ferrari/raku/ch-2.p6 | 15 |
12 files changed, 177 insertions, 0 deletions
diff --git a/challenge-194/luca-ferrari/blog-1.txt b/challenge-194/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..db68590932 --- /dev/null +++ b/challenge-194/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/05/PerlWeeklyChallenge194.html#task1 diff --git a/challenge-194/luca-ferrari/blog-2.txt b/challenge-194/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..5da9cb8427 --- /dev/null +++ b/challenge-194/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/05/PerlWeeklyChallenge194.html#task2 diff --git a/challenge-194/luca-ferrari/blog-3.txt b/challenge-194/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..a866a4f6d6 --- /dev/null +++ b/challenge-194/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/05/PerlWeeklyChallenge194.html#task1plperl diff --git a/challenge-194/luca-ferrari/blog-4.txt b/challenge-194/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..5cd8f9ea87 --- /dev/null +++ b/challenge-194/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/05/PerlWeeklyChallenge194.html#task2plperl diff --git a/challenge-194/luca-ferrari/blog-5.txt b/challenge-194/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..eb88ce35f6 --- /dev/null +++ b/challenge-194/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/05/PerlWeeklyChallenge194.html#task1plpgsql diff --git a/challenge-194/luca-ferrari/blog-6.txt b/challenge-194/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..e9f7076c78 --- /dev/null +++ b/challenge-194/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/05/PerlWeeklyChallenge194.html#task2plpgsql diff --git a/challenge-194/luca-ferrari/postgresql/ch-1.plperl b/challenge-194/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..62445dd0df --- /dev/null +++ b/challenge-194/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,31 @@ +-- Perl Weekly Challenge 194 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc194; + +CREATE OR REPLACE FUNCTION +pwc194.task1_plperl( text ) +RETURNS int +AS $CODE$ + my ($what) = @_; + + if ( $what =~ / ^ ([\d?]) ([\d?]) : ([\d?]) ([\d?]) $ /x ) { + if ( $1 eq '?' ) { + return 9; + } + elsif ( $2 eq '?' ) { + return 3 if $1 == 2; + return 9; + } + elsif ( $3 eq '?' ) { + return 5; + } + else { + return 9; + } + } + +return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-194/luca-ferrari/postgresql/ch-1.sql b/challenge-194/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..848fc5989c --- /dev/null +++ b/challenge-194/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,39 @@ +-- Perl Weekly Challenge 194 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc194; + +CREATE OR REPLACE FUNCTION +pwc194.task1_plpgsql( what text ) +RETURNS int +AS $CODE$ +DECLARE + needle char; + idx int := 0; + prev char; +BEGIN + FOREACH needle IN ARRAY regexp_match( what, '^([\d?])([\d?]):([\d?])([\d?])$' ) LOOP + IF needle <> '?' THEN + idx := idx + 1; + prev := needle; + CONTINUE; + END IF; + + IF idx = 0 THEN + RETURN 2; + ELSEIF idx = 1 THEN + IF prev = '2' THEN + RETURN 3; + ELSE + RETURN 9; + END IF; + ELSEIF idx = 2 THEN + RETURN 5; + ELSE + RETURN 9; + END IF; + END LOOP; +RETURN NULL; +END +$CODE$ +LANGUAGE plpgsql; 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; diff --git a/challenge-194/luca-ferrari/postgresql/ch-2.sql b/challenge-194/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..e918f5b89b --- /dev/null +++ b/challenge-194/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,46 @@ +-- Perl Weekly Challenge 194 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc194; + +CREATE OR REPLACE FUNCTION +pwc194.task2_plpgsql( what text) +RETURNS int +AS $CODE$ +DECLARE + t text; + current_max int; + current_min int; + current_count int; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS counter ( l char, c int, PRIMARY KEY(l) ); + TRUNCATE counter; + + FOR t IN SELECT v FROM regexp_split_to_table( what, '' ) v LOOP + INSERT INTO counter AS cnt ( l, c ) + VALUES ( t, 1 ) + ON CONFLICT (l) + DO UPDATE SET c = cnt.c + 1; + END LOOP; + + SELECT max(c), min(c) + INTO current_max, current_min + FROM counter; + + IF current_max - current_min <> 1 THEN + RETURN 0; + END IF; + + SELECT count(*) + INTO current_count + FROM counter + WHERE c = current_max; + + IF current_count <> 1 THEN + RETURN 0; + END IF; + + RETURN 1; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-194/luca-ferrari/raku/ch-1.p6 b/challenge-194/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..ef5abaf253 --- /dev/null +++ b/challenge-194/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,14 @@ +#!raku + +# Perl Weekly Challenge 194 + +sub MAIN( Str $what ) { + + given ( $what ) { + when ( / ^ \? \d ':' \d ** 2 $ / ) { 2.say and exit } + when ( / ^ <[01]> \? ':' \d ** 2 $ / ) { 9.say and exit } + when ( / ^ 2 \? ':' \d ** 2 $ / ) { 3.say and exit } + when ( / ^ \d ** 2 ':' \? \d $ / ) { 5.say and exit } + when ( / ^\d ** 2 ':' \d \? $ / ) { 9.say and exit } + } +} diff --git a/challenge-194/luca-ferrari/raku/ch-2.p6 b/challenge-194/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..885e45c93f --- /dev/null +++ b/challenge-194/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,15 @@ +#!raku + +# Perl Weekly Challenge 194 + +sub MAIN( Str $what where { $what ~~ / ^ <[a..z]>+ $ / } ) { + + my $counter = Bag.new: $what.comb; + + "1".say and exit if ( $counter.values.max - $counter.values.min == 1 + && $counter.keys.grep( { $counter{ $_ } == $counter.values.max } ) == 1 ); + + "0".say; + + +} |
