aboutsummaryrefslogtreecommitdiff
path: root/challenge-145
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-29 10:47:32 +0000
committerGitHub <noreply@github.com>2021-12-29 10:47:32 +0000
commite3f085c3a0cf77afda804c262df2474ec65e0571 (patch)
treec1e0fcf0171dab956ccfdb49942a2b78de4985ab /challenge-145
parente304633e4909c86967b059e9c8296aa3603f9960 (diff)
parent69bfd1b36161319204898130bf88961bc873a3d1 (diff)
downloadperlweeklychallenge-club-e3f085c3a0cf77afda804c262df2474ec65e0571.tar.gz
perlweeklychallenge-club-e3f085c3a0cf77afda804c262df2474ec65e0571.tar.bz2
perlweeklychallenge-club-e3f085c3a0cf77afda804c262df2474ec65e0571.zip
Merge pull request #5438 from fluca1978/pwc145
Pwc145
Diffstat (limited to 'challenge-145')
-rw-r--r--challenge-145/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-145/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-145/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-145/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-145/luca-ferrari/postgresql/ch-1.sql23
-rw-r--r--challenge-145/luca-ferrari/postgresql/ch-2.sql50
-rwxr-xr-xchallenge-145/luca-ferrari/raku/ch-1.p65
-rwxr-xr-xchallenge-145/luca-ferrari/raku/ch-2.p626
8 files changed, 108 insertions, 0 deletions
diff --git a/challenge-145/luca-ferrari/blog-1.txt b/challenge-145/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..d68d3fe0a0
--- /dev/null
+++ b/challenge-145/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/12/29/PerlWeeklyChallenge145.html#task1
diff --git a/challenge-145/luca-ferrari/blog-2.txt b/challenge-145/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..0c95ca45ac
--- /dev/null
+++ b/challenge-145/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/12/29/PerlWeeklyChallenge145.html#task2
diff --git a/challenge-145/luca-ferrari/blog-3.txt b/challenge-145/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..87ffab3d58
--- /dev/null
+++ b/challenge-145/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/12/29/PerlWeeklyChallenge145.html#task1pg
diff --git a/challenge-145/luca-ferrari/blog-4.txt b/challenge-145/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..0ec276e5ad
--- /dev/null
+++ b/challenge-145/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/12/29/PerlWeeklyChallenge145.html#task2pg
diff --git a/challenge-145/luca-ferrari/postgresql/ch-1.sql b/challenge-145/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..bfa9121355
--- /dev/null
+++ b/challenge-145/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,23 @@
+/*
+testdb=> select f_dot_product( array[1,2,3], array[4,5,6] );
+f_dot_product
+---------------
+32
+*/
+CREATE OR REPLACE FUNCTION
+f_dot_product( a int[], b int[] )
+RETURNS int
+AS
+$CODE$
+DECLARE
+ i int;
+ total int := 0;
+BEGIN
+ FOR i IN 1 .. array_length( a, 1 ) LOOP
+ total := total + a[ i ] * b[ i ];
+ END LOOP;
+
+ RETURN total;
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-145/luca-ferrari/postgresql/ch-2.sql b/challenge-145/luca-ferrari/postgresql/ch-2.sql
new file mode 100644
index 0000000000..0319ca15d8
--- /dev/null
+++ b/challenge-145/luca-ferrari/postgresql/ch-2.sql
@@ -0,0 +1,50 @@
+/**
+testdb=> select * from f_eertree( 'redivider' );
+current_root | string
+--------------+-----------
+r | redivider
+e | edivide
+d | divid
+i | ivi
+(4 rows)
+
+testdb=> select * from f_eertree( 'hello' );
+current_root | string
+--------------+--------
+l | ll
+(1 row)
+
+*/
+CREATE OR REPLACE FUNCTION
+f_eertree( s text )
+RETURNS TABLE( current_root char, string text )
+AS $CODE$
+DECLARE
+ current int;
+ other int;
+ other_root char;
+BEGIN
+
+ FOR current IN 1 .. length( s ) LOOP
+ current_root := substring( s FROM current FOR 1 );
+
+ FOR other IN current + 1 .. length( s ) LOOP
+ other_root := substring( s FROM other FOR 1 );
+ IF other_root <> current_root THEN
+ CONTINUE;
+ END IF;
+
+ string := substring( s, current, other - current + 1 );
+
+ IF string = reverse( string ) THEN
+ RETURN NEXT;
+ END IF;
+
+ END LOOP;
+ END LOOP;
+
+ RETURN;
+
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-145/luca-ferrari/raku/ch-1.p6 b/challenge-145/luca-ferrari/raku/ch-1.p6
new file mode 100755
index 0000000000..6cffb67a38
--- /dev/null
+++ b/challenge-145/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,5 @@
+#!raku
+
+sub MAIN( *@n where { @n.elems %% 2 && @n.grep( * ~~ Int ).elems == @n.elems } ) {
+ ( [+] ( @n[ 0 .. @n.elems / 2 - 1 ] Z* @n[ @n.elems / 2 .. * - 1 ] ) ).say;
+}
diff --git a/challenge-145/luca-ferrari/raku/ch-2.p6 b/challenge-145/luca-ferrari/raku/ch-2.p6
new file mode 100755
index 0000000000..97202d578c
--- /dev/null
+++ b/challenge-145/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,26 @@
+#!raku
+sub MAIN( Str $s = 'redivider' ) {
+
+ my @roots;
+ my @chars = $s.comb;
+
+ for 0 ..^ @chars.elems -> $current {
+ my $current-root = @chars[ $current ];
+
+
+ for $current + 1 ..^ @chars.elems -> $other {
+ next if @chars[ $other ] !~~ $current-root;
+
+ my $string = @chars[ $current .. $other ].join;
+
+ if ( $string ~~ $string.flip ) {
+ @roots.push: [ $current-root, $string ];
+ last;
+ }
+ }
+
+ @roots.push: [ $current-root, '' ] if ! @roots.grep({ $_[ 0 ] ~~ $current-root } );
+ }
+
+ "$_[0] = $_[1]".say for @roots;
+}