diff options
| -rw-r--r-- | challenge-080/dave-jacoby/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-080/dave-jacoby/node/ch-1.js | 25 | ||||
| -rwxr-xr-x | challenge-080/dave-jacoby/node/ch-2.js | 22 |
3 files changed, 48 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; +} + + |
