diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-11 21:51:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-11 21:51:30 +0100 |
| commit | 3e6718dd8ff06074bec334a291b74b31b8db9305 (patch) | |
| tree | b20ec685f0f56c272820926a1823fff02a883f40 | |
| parent | 248f511c3258438906041e5b03000e9678a6bee1 (diff) | |
| parent | 645d483178b78bec308d687439d201c1779b9373 (diff) | |
| download | perlweeklychallenge-club-3e6718dd8ff06074bec334a291b74b31b8db9305.tar.gz perlweeklychallenge-club-3e6718dd8ff06074bec334a291b74b31b8db9305.tar.bz2 perlweeklychallenge-club-3e6718dd8ff06074bec334a291b74b31b8db9305.zip | |
Merge pull request #4692 from stuart-little/stuart-little_125_node
1st commit on 125_node
| -rwxr-xr-x | challenge-125/stuart-little/node/ch-1.js | 27 | ||||
| -rwxr-xr-x | challenge-125/stuart-little/node/ch-2.js | 51 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-125/stuart-little/node/ch-1.js b/challenge-125/stuart-little/node/ch-1.js new file mode 100755 index 0000000000..e779fc8023 --- /dev/null +++ b/challenge-125/stuart-little/node/ch-1.js @@ -0,0 +1,27 @@ +#!/usr/bin/env node + +// run <script> <number> + +function trps(n) { + let triples=[] + for (let i=1; i<=Math.floor((n+1)/2); i++) { + let sqDif = Math.floor(Math.sqrt(n**2-i**2)) + if ((i<n) && (i**2+sqDif**2==n**2)) { + triples.push([i,sqDif,n]) + } + } + for (let i=1; i<n; i++) { + if ((n**2 % i == 0) && ((i%2) === ((Math.floor(n**2/i))%2))) { + let s = Math.floor(n**2/i) + triples.push([Math.floor((s-i)/2), n, Math.floor((s+i)/2)]) + } + } + return triples +} + +const sol = trps(parseInt(process.argv[2])); +if (sol.length>0) { + sol.forEach(x => console.log(x)); + process.exit() +} +console.log(-1) diff --git a/challenge-125/stuart-little/node/ch-2.js b/challenge-125/stuart-little/node/ch-2.js new file mode 100755 index 0000000000..500019c150 --- /dev/null +++ b/challenge-125/stuart-little/node/ch-2.js @@ -0,0 +1,51 @@ +#!/usr/bin/env node + +const { maxBy } = require('lodash') + +function lr(tree) { + if (tree.length < 3 || tree[0] === '.') { return [[],[]] } + if (tree.length === 3) { return [["."],["."]] } + left=[] + let [sm,ix] = [0,1] + while (sm !== -1) { + left.push(tree[ix]) + sm+=(tree[ix] == '.' ? -1 : 1) + ix+=1 + } + return [left,tree.slice(left.length+1)] +} + +function lrLongPath(tree) { + if (tree[0] === '.') { return [[],[]] } + if (tree.length === 3) { return [[tree[0]],[tree[0]]] } + return lr(tree).map( ar => [tree[0], ...maxBy(lrLongPath(ar), x => x.length)] ) +} + +function biLongPath(tree) { + if (tree.length < 3 || tree[0] == '.') { return [] } + if (tree.length === 3) { return [tree[0]] } + let [lPath,rPath] = lrLongPath(tree) + let path = [...([...(lPath.slice(1))].reverse()),...rPath] + let [left,right] = lr(tree) + return maxBy([path, ...[left,right].map(biLongPath)], x => x.length) +} + +console.log(biLongPath(process.argv.slice(2)).join(" ")) + +/* +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 +*/ |
