diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-26 21:40:08 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-26 21:40:08 +0000 |
| commit | 536dafb989fad8da4c2df0df1bfc22eb2fac3706 (patch) | |
| tree | c2b39c685cca7cc2ed3b4ab4e91e75f5b9a71fdd /challenge-002/abigail/node | |
| parent | e36daeee6e93355383ad3a1c3fc43271f9a357d7 (diff) | |
| parent | c34bb5d7bd7fce08e8311a0f527ce7fbd69e4dae (diff) | |
| download | perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.tar.gz perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.tar.bz2 perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-002/abigail/node')
| -rw-r--r-- | challenge-002/abigail/node/ch-1.js | 14 | ||||
| -rw-r--r-- | challenge-002/abigail/node/ch-2.js | 84 |
2 files changed, 98 insertions, 0 deletions
diff --git a/challenge-002/abigail/node/ch-1.js b/challenge-002/abigail/node/ch-1.js new file mode 100644 index 0000000000..5146e85366 --- /dev/null +++ b/challenge-002/abigail/node/ch-1.js @@ -0,0 +1,14 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => console . log (+_)) +; diff --git a/challenge-002/abigail/node/ch-2.js b/challenge-002/abigail/node/ch-2.js new file mode 100644 index 0000000000..84d0f6976e --- /dev/null +++ b/challenge-002/abigail/node/ch-2.js @@ -0,0 +1,84 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js {-f | -t} < input-file +// +// -f: Translate from base 35 to base 10 +// -t: Translate to base 35 from base 10 +// + +let BASE = 35 + +// +// Parse options using the yargs module +// +const argv = require ('yargs') +. option ('from_base', { + alias: 'f', + type: 'boolean', +}) +. option ('to_base', { + alias: 't', + type: 'boolean', +}) +. argv; + +// +// Check options +// +if ((argv . to_base ? 1 : 0) + (argv . from_base ? 1 : 0) != 1) { + console . log ("Requires exactly one of '-f' or '-t'") + process . exit (1) +} + +// +// Set up digits, mapping base-10 numbers to base-35 digits, and vice versa +// +let digits = {}; +for (let i = 0; i < 10; i ++) { + digits [i] = i +} +for (let i = 10; i < BASE; i ++) { + let char = String . fromCharCode (65 + i - 10) + digits [char] = i + digits [i] = char +} + +// +// Translate to base 35 +// +function to_base (number) { + let out = ""; + while (number) { + let n = number % BASE + out = digits [n] + out + number = Math . floor (number / BASE) + } + return out +} + +// +// Translate from base 35 +// +function from_base (base) { + let out = 0 + base . split ('') . forEach (c => { + out = BASE * out + digits [c] + }) + return out +} + + +// +// Iterate over the input, call either to_base or from_base for each +// line, depending on the -f or -t parameter. +// +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => console . log (argv . to_base ? to_base (+ _) + : from_base (_ . trim ()))) +; |
