aboutsummaryrefslogtreecommitdiff
path: root/challenge-080/dave-jacoby
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-10-10 18:34:15 +0800
committer冯昶 <seaker@qq.com>2020-10-10 18:34:15 +0800
commitf0a88ea2365d8ae7dfb90c969d83368a18b53f9a (patch)
tree64665b115f1023519b48f3f366bb743ab68375d8 /challenge-080/dave-jacoby
parent353a66fe9f75298e44b27d692109259646e7d09f (diff)
parenta8ea1576ec8f1801e4c90d906c5d3c18ebde5ca6 (diff)
downloadperlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.tar.gz
perlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.tar.bz2
perlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-080/dave-jacoby')
-rw-r--r--challenge-080/dave-jacoby/blog.txt1
-rwxr-xr-xchallenge-080/dave-jacoby/node/ch-1.js25
-rwxr-xr-xchallenge-080/dave-jacoby/node/ch-2.js22
-rwxr-xr-xchallenge-080/dave-jacoby/perl/ch-1.pl26
-rwxr-xr-xchallenge-080/dave-jacoby/perl/ch-2.pl29
5 files changed, 103 insertions, 0 deletions
diff --git a/challenge-080/dave-jacoby/blog.txt b/challenge-080/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..95ae4e56fd
--- /dev/null
+++ b/challenge-080/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2020/09/30/challenge-80.html
diff --git a/challenge-080/dave-jacoby/node/ch-1.js b/challenge-080/dave-jacoby/node/ch-1.js
new file mode 100755
index 0000000000..6e23ec8521
--- /dev/null
+++ b/challenge-080/dave-jacoby/node/ch-1.js
@@ -0,0 +1,25 @@
+"use strict"
+
+console.log( spnm( [5, 2, -2, 0 ] ));
+console.log( spnm( [1, 8, -1 ] ));
+console.log( spnm( [2, 0, -1 ] ));
+console.log( spnm( Array(12).fill().map((n, i) => 1 + i) ));
+
+function spnm ( array ) {
+ let list = array.filter( i => i > 0 );
+ let max = 1 + Math.max(...list);
+ let range = Array(max).fill().map((n, i) => 1 + i) ;
+ let hash = {};
+
+ for ( let i in list ) {
+ let n = list[i];
+ hash[n]=1;
+ }
+
+ for ( let i in range ) {
+ let n = range[i];
+ if ( !hash[n] ) { return n }
+ }
+ return -1
+}
+
diff --git a/challenge-080/dave-jacoby/node/ch-2.js b/challenge-080/dave-jacoby/node/ch-2.js
new file mode 100755
index 0000000000..a47fe0eb2b
--- /dev/null
+++ b/challenge-080/dave-jacoby/node/ch-2.js
@@ -0,0 +1,22 @@
+"use strict"
+
+console.log( candy_count( [1, 2, 2] ) );
+console.log( candy_count( [1, 4, 3, 2] ) );
+
+
+function candy_count( candidates ) {
+ let total = 0;
+ console.log( candidates );
+ for ( let i in candidates ) {
+ i = parseInt(i); // string by default
+ let v = candidates[i];
+ let prev = candidates[i-1] || 0;
+ let next = candidates[i+1] || 0;
+ total ++;
+ if ( v > prev && prev != 0 ) { total ++ }
+ if ( v > next && next != 0 ) { total ++ }
+ }
+ return total;
+}
+
+
diff --git a/challenge-080/dave-jacoby/perl/ch-1.pl b/challenge-080/dave-jacoby/perl/ch-1.pl
new file mode 100755
index 0000000000..98df132442
--- /dev/null
+++ b/challenge-080/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+use Carp;
+use List::Util qw{ min max };
+use Getopt::Long;
+
+say spnm( 5, 2, -2, 0 ); # 1
+say spnm( 1, 8, -1 ); # 2
+say spnm( 2, 0, -1 ); # 1
+say spnm( 0 .. 12 ); # 13
+
+sub spnm( @array ) {
+ my @list = grep { $_ > 0 } @array; # list to only positive nums
+ my %list = map { $_ => 1 } @list; # hash for easy lookup
+ my $max = 1 + max @list; # highest potential missing number
+ for my $i ( 1 .. $max ) { # starting from lowest potential
+ return $i unless $list{$i}; # return if no in lookup
+ }
+ return -1; # just in case
+}
+
diff --git a/challenge-080/dave-jacoby/perl/ch-2.pl b/challenge-080/dave-jacoby/perl/ch-2.pl
new file mode 100755
index 0000000000..b193067442
--- /dev/null
+++ b/challenge-080/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+say candy_count( 1, 2, 2 );
+say candy_count( 1, 4, 3, 2 );
+
+# a) You must given at least one candy to each candidate.
+# b) Candidate with higher ranking get more candies than
+# their immediate neighbors on either side.
+sub candy_count ( @n ) {
+ say join '', map { '-' } @n;
+ say join ' ', @n;
+ my $total = 0;
+
+ for my $i ( 0 .. $#n ) {
+ my $v = $n[$i];
+ my $prev = $n[ $i - 1 ] || 0;
+ my $next = $n[ $i + 1 ] || 0;
+ $total++; # rule A
+ $total++ if $v > $prev && $prev > 0; # rule B.1
+ $total++ if $v > $next && $next > 0; # rule B.2
+ }
+ say '';
+ return $total;
+}