aboutsummaryrefslogtreecommitdiff
path: root/challenge-073
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-10 10:57:23 +0100
committerGitHub <noreply@github.com>2020-08-10 10:57:23 +0100
commit6396feb7b85e86323f9ef0bf890094db25b39963 (patch)
treecfcbbb1af12d34ffb7f2f1d0ce96feba24d1117c /challenge-073
parent7627bc0c643f2642f217330ff71fdeef04a079a5 (diff)
parent80ae88a108608ff7087b5b2893c0ed998f73086f (diff)
downloadperlweeklychallenge-club-6396feb7b85e86323f9ef0bf890094db25b39963.tar.gz
perlweeklychallenge-club-6396feb7b85e86323f9ef0bf890094db25b39963.tar.bz2
perlweeklychallenge-club-6396feb7b85e86323f9ef0bf890094db25b39963.zip
Merge pull request #2059 from shawnw/challenge-073-solution
Challenge 073 solution, both tasks
Diffstat (limited to 'challenge-073')
-rw-r--r--challenge-073/shawn-wagner/README1
-rwxr-xr-xchallenge-073/shawn-wagner/perl/ch-1.pl29
-rwxr-xr-xchallenge-073/shawn-wagner/perl/ch-2.pl31
-rwxr-xr-xchallenge-073/shawn-wagner/tcl/ch-1.tcl14
-rwxr-xr-xchallenge-073/shawn-wagner/tcl/ch-2.tcl20
5 files changed, 95 insertions, 0 deletions
diff --git a/challenge-073/shawn-wagner/README b/challenge-073/shawn-wagner/README
new file mode 100644
index 0000000000..8133463776
--- /dev/null
+++ b/challenge-073/shawn-wagner/README
@@ -0,0 +1 @@
+Solution by Shawn Wagner
diff --git a/challenge-073/shawn-wagner/perl/ch-1.pl b/challenge-073/shawn-wagner/perl/ch-1.pl
new file mode 100755
index 0000000000..ab7bc90186
--- /dev/null
+++ b/challenge-073/shawn-wagner/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use feature qw/signatures say/;
+no warnings qw/experimental/;
+use DBI;
+
+# This is really just a thin layer of perl over SQLite to solve the
+# problems using SQL window functions.
+
+sub task1 ($A, $S) :prototype(\@$) {
+ my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:","","");
+ $dbh->do("CREATE TABLE data(id INTEGER PRIMARY KEY, val INTEGER)");
+ my $stmt = $dbh->prepare("INSERT INTO data(val) VALUES (?)");
+ $stmt->execute_array({}, $A);
+ $S -= 1;
+ my $len = @$A - $S;
+ my $nums = $dbh->selectall_arrayref(<<EOS);
+SELECT min(val) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND $S FOLLOWING)
+FROM data
+ORDER BY id
+LIMIT $len
+EOS
+ say "Task 1:\t", join(", ", map { $_->[0] } @$nums);
+}
+
+my @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8);
+my $S = 3;
+task1 @A, $S;
diff --git a/challenge-073/shawn-wagner/perl/ch-2.pl b/challenge-073/shawn-wagner/perl/ch-2.pl
new file mode 100755
index 0000000000..a6386b71fa
--- /dev/null
+++ b/challenge-073/shawn-wagner/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use feature qw/say/;
+use DBI;
+
+# This is really just a thin layer of perl over SQLite to solve the
+# problems using SQL window functions.
+
+sub task2 {
+ my @A = @_;
+ my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:","","");
+ $dbh->do("CREATE TABLE data(id INTEGER PRIMARY KEY, val INTEGER)");
+ my $stmt = $dbh->prepare("INSERT INTO data(val) VALUES (?)");
+ $stmt->execute_array({}, \@A);
+
+ my $nums = $dbh->selectall_arrayref(<<EOS);
+SELECT CASE
+ WHEN min(val) OVER w < val THEN min(val) OVER w
+ ELSE 0
+ END
+FROM data
+WINDOW w AS (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
+ORDER BY id
+EOS
+
+ say "Task 2:\t", join(", ", map { $_->[0] } @$nums);
+}
+
+task2 7, 8, 3, 12, 10;
+task2 4, 6, 5;
diff --git a/challenge-073/shawn-wagner/tcl/ch-1.tcl b/challenge-073/shawn-wagner/tcl/ch-1.tcl
new file mode 100755
index 0000000000..79bf8f60cf
--- /dev/null
+++ b/challenge-073/shawn-wagner/tcl/ch-1.tcl
@@ -0,0 +1,14 @@
+#!/usr/bin/env tclsh
+package require Tcl 8.6
+
+proc task1 {A S} {
+ incr S -1
+ set last_index [expr {[llength $A] - $S}]
+ for {set i 0} {$i < $last_index} {incr i} {
+ lappend results [::tcl::mathfunc::min {*}[lrange $A $i $i+$S]]
+ }
+ puts "Task 1:\t$results"
+}
+
+task1 {1 5 0 2 9 3 7 6 4 8} 3
+
diff --git a/challenge-073/shawn-wagner/tcl/ch-2.tcl b/challenge-073/shawn-wagner/tcl/ch-2.tcl
new file mode 100755
index 0000000000..3f6f8d7daf
--- /dev/null
+++ b/challenge-073/shawn-wagner/tcl/ch-2.tcl
@@ -0,0 +1,20 @@
+#!/usr/bin/env tclsh
+package require Tcl 8.6
+
+proc task2 {A} {
+ set last_index [llength $A]
+ set results [list 0]
+ for {set i 1} {$i < $last_index} {incr i} {
+ set m [::tcl::mathfunc::min {*}[lrange $A 0 $i-1]]
+ if {$m < [lindex $A $i]} {
+ lappend results $m
+ } else {
+ lappend results 0
+ }
+ }
+ puts "Task 2:\t$results"
+}
+
+task2 {7 8 3 12 10}
+task2 {4 6 5}
+