aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-18 13:45:19 +0100
committerGitHub <noreply@github.com>2021-05-18 13:45:19 +0100
commit81d709a2aa3f88844414d2cadaf2728f964e291a (patch)
treeedf5b7d5a4b94b19301b085d3210c59941518d15
parentf15610b151516c70d5c92cccf88bf65241b12673 (diff)
parenta2ff7b545c1f1dce06040fd3954876c2e8cb49bf (diff)
downloadperlweeklychallenge-club-81d709a2aa3f88844414d2cadaf2728f964e291a.tar.gz
perlweeklychallenge-club-81d709a2aa3f88844414d2cadaf2728f964e291a.tar.bz2
perlweeklychallenge-club-81d709a2aa3f88844414d2cadaf2728f964e291a.zip
Merge pull request #4105 from stuart-little/stuart-little_113_node
1st commit on 113_node
-rwxr-xr-xchallenge-113/stuart-little/node/ch-1.js20
-rwxr-xr-xchallenge-113/stuart-little/node/ch-2.js49
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-113/stuart-little/node/ch-1.js b/challenge-113/stuart-little/node/ch-1.js
new file mode 100755
index 0000000000..319423ece6
--- /dev/null
+++ b/challenge-113/stuart-little/node/ch-1.js
@@ -0,0 +1,20 @@
+#!/usr/bin/env node
+
+// run <script> <digit>
+
+function lastDigSumm(nr, dig, nrSummands) {
+ return ((nr - nrSummands * dig) % 10 == 0) && (nrSummands * dig <= nr) && (nrSummands * ((dig -1) * 10 + dig) >= nr)
+}
+
+function lastDig(nr,dig) {
+ return [...Array(9)].map((_,i) => i+1).filter(x => lastDigSumm(nr,dig,x)).length >= 1
+}
+
+function sol(nr,dig) {
+ if (dig == 0) {
+ return (nr >= 101 || (nr % 10 == 0))
+ }
+ return ((nr >= dig * 11) || lastDig(nr,dig))
+}
+
+console.log(sol(...process.argv.slice(2).map(x => parseInt(x))) ? 1 : 0)
diff --git a/challenge-113/stuart-little/node/ch-2.js b/challenge-113/stuart-little/node/ch-2.js
new file mode 100755
index 0000000000..2364b0a3a8
--- /dev/null
+++ b/challenge-113/stuart-little/node/ch-2.js
@@ -0,0 +1,49 @@
+#!/usr/bin/env node
+
+const printTree = require('print-tree');
+
+function split2trees(lst) {
+ ix = [...lst.keys()].find(i => 2*lst.slice(0,i+1).filter(x => x === ".").length > i+1 );
+ return [lst.slice(0,ix+1), lst.slice(ix+1)]
+}
+
+function mkTree(lst) {
+ if (lst[0] === ".") {
+ return { name: "" };
+ };
+ const nm = lst[0];
+ const [lft,rght] = split2trees(lst.slice(1));
+ return {
+ name: nm,
+ children: [mkTree(lft),mkTree(rght)],
+ };
+}
+
+const inTreeList = (process.argv.length > 2) ? (process.argv.slice(2)) : (["1", "2", "4", ".", "7", ".", ".", ".", "3", "5", ".", ".", "6", ".", "."]);
+const sm = inTreeList.reduce((acc,el) => {return acc + (parseInt(el) || 0)}, 0);
+const outTreeList = inTreeList.map(x => (parseInt(x)) ? (sm - parseInt(x)) : x);
+
+printTree(
+ mkTree(outTreeList),
+ node => node.name.toString(),
+ node => node.children,
+);
+
+/*
+run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values>
+
+ref: https://stackoverflow.com/a/2676849/11064961
+
+e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree
+
+ 1
+ / \
+ 2 3
+ / / \
+ 4 5 6
+ \
+ 7
+
+given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2
+*/
+