diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-21 22:21:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-21 22:21:41 +0100 |
| commit | 83d6bae247ba7a3e475bda546c6e92bfce5b5fd7 (patch) | |
| tree | 331391826090222b6c0a038803955a6ac1cc5e5e /challenge-079/abigail/node | |
| parent | 4ea66a8bc51115f617734bf1e2d3e221dafc2c5f (diff) | |
| parent | 88acc407f4db3e21ac6701f49886728f2dc846b2 (diff) | |
| download | perlweeklychallenge-club-83d6bae247ba7a3e475bda546c6e92bfce5b5fd7.tar.gz perlweeklychallenge-club-83d6bae247ba7a3e475bda546c6e92bfce5b5fd7.tar.bz2 perlweeklychallenge-club-83d6bae247ba7a3e475bda546c6e92bfce5b5fd7.zip | |
Merge pull request #2340 from Abigail/abigail/week-079
Abigail/week 079
Diffstat (limited to 'challenge-079/abigail/node')
| -rw-r--r-- | challenge-079/abigail/node/ch-1.js | 53 | ||||
| -rw-r--r-- | challenge-079/abigail/node/ch-2.js | 77 |
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-079/abigail/node/ch-1.js b/challenge-079/abigail/node/ch-1.js new file mode 100644 index 0000000000..6c9ca09ace --- /dev/null +++ b/challenge-079/abigail/node/ch-1.js @@ -0,0 +1,53 @@ +// +// Challenge: +// +// You are given a positive number $N. +// +// Write a script to count the total numbrer of set bits of the binary +// representations of all numbers from 1 to $N and return +// $total_count_set_bit % 1000000007. +// +// https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/ +// + +// +// This is A000778 (https://oeis.org/A000788). There's a recursive formala +// for the number of bits in the binary representation of 0 .. $N: +// +// bits (0) = 0 +// bits (2 * N) = bits (N) + bits (N - 1) + N +// bits (2 * N + 1) = 2 * bits (N) + N + 1 +// + +// +// Create an interface to read from STDIN +// +const rl = require ('readline') . createInterface ({ + input: process . stdin, +}); + +// +// Read lines of input, calculate the result, and print it. +// +rl . on ('line', (line) => { + console . log (bits (+line)); // Unary + numifies +}); + + +// +// Recursive function to calculate the results (see above). +// +let cache = []; +function bits (n) { + if (n == 0) { + return 0; + } + + if (!cache [n]) { + let half = Math . floor (n / 2); + cache [n] = bits (half) + half + + (n % 2 ? bits (half) + 1 : bits (half - 1)); + } + return cache [n]; +} + diff --git a/challenge-079/abigail/node/ch-2.js b/challenge-079/abigail/node/ch-2.js new file mode 100644 index 0000000000..a3de411e8c --- /dev/null +++ b/challenge-079/abigail/node/ch-2.js @@ -0,0 +1,77 @@ +// +// Challenge: +// +// You are given an array of positive numbers @N. +// Write a script to represent it as Histogram Chart and find out +// how much water it can trap. +// + +// +// Create an interface to read from STDIN +// +const rl = require ('readline') . createInterface ({ + input: process . stdin, +}); + +// +// Read lines of input, calculate the result, and print it. +// +rl . on ('line', (line) => { + print_histogram (line); +}); + + +// +// Node doesn't have (s)printf, so we're writing our own formatting function. +// +function ff (n, width, pad = " ") { + let s = n . toString (); + while (s . length < width) { + s = pad + s; + } + return s; +} + +function print_histogram (input) { + // + // Extract N from the input, find the maximum, + // and the width of this maximum. + // + let N = input . split (" ") . map (Number); + let max = Math . max (... N); + let w = max . toString () . length; + + // + // Count down volumes from the maximum, down to 1. + // For each volume, print the volume, and for each entry + // in N, print "#" if the volume is equal or less than + // the entry in N, else, print " ". + // + for (let volume = max; volume; volume --) { + let line = ff (volume, w); + for (let i = 0; i < N . length; i ++) { + line += " " + ff (volume <= N [i] ? "#" : " ", w); + } + console . log (line); + } + + // + // Print the bars + // + let line = ""; + line += ff ("_", w, "_"); + for (let i = 0; i < N . length; i ++) { + line += " " + ff ("_", w, "_"); + } + console . log (line); + + // + // Finally, print the totals + // + line = ""; + line += ff (" ", w); + for (let i = 0; i < N . length; i ++) { + line += " " + ff (N [i], w); + } + console . log (line); +} |
