aboutsummaryrefslogtreecommitdiff
path: root/challenge-078/abigail/node
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-078/abigail/node')
-rw-r--r--challenge-078/abigail/node/ch-1.js52
-rw-r--r--challenge-078/abigail/node/ch-2.js33
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-078/abigail/node/ch-1.js b/challenge-078/abigail/node/ch-1.js
new file mode 100644
index 0000000000..eba11fd3b3
--- /dev/null
+++ b/challenge-078/abigail/node/ch-1.js
@@ -0,0 +1,52 @@
+//
+// Exercise:
+// You are given an array @A containing distinct integers.
+// Write a script to find all leader elements in the array @A.
+// Print (0) if none found. An element is leader if it is greater
+// than all the elements to its right side.
+//
+
+//
+// Note:
+// - The only way no leader element can be found is if the array is empty.
+// - We will read the array from STDIN.
+//
+
+//
+// Read a line from STDIN, turn it into a string, strip off the
+// trailing newline (and any leading or trailing whitespace),
+// and then split in on spaces. Store the result into an array "arry".
+//
+let fs = require ("fs");
+let line = fs . readFileSync (0) . toString () . trim ();
+let arry = line . split (" ");
+
+if (line . length == 0) {
+ //
+ // Special case, if the line is empty, output 0
+ //
+ console . log (0);
+}
+else {
+ //
+ // Iterate backwards over the array. Keep track of the largest
+ // element so far in an array 'out'. If we find a new largest
+ // element, put this first in 'out' (so, the largest element
+ // seen so far is always in 'out [0]'.
+ //
+ let out = [arry [arry . length - 1]];
+ for (let i = arry . length - 2; i >= 0; i --) {
+ //
+ // Note that we have strings in arry (and hence, out).
+ // An unary + casts them to numbers.
+ //
+ if (+arry [i] > +out [0]) {
+ out . unshift (arry [i]);
+ }
+ }
+
+ //
+ // Print the result.
+ //
+ console . log (out . join (" "));
+}
diff --git a/challenge-078/abigail/node/ch-2.js b/challenge-078/abigail/node/ch-2.js
new file mode 100644
index 0000000000..6d76c9aa91
--- /dev/null
+++ b/challenge-078/abigail/node/ch-2.js
@@ -0,0 +1,33 @@
+//
+// Exercise:
+//
+// You are given array @A containing positive numbers and @B containing
+// one or more indices from the array @A.
+// Write a script to left rotate @A so that the number at the first index
+// of @B becomes the first element in the array. Similary, left rotate @A
+// again so that the number at the second index of @B becomes the first
+// element in the array.
+//
+// We will be reading the arrays from STDIN -- @A is one the first
+// line, @B is on the second line.
+//
+
+//
+// Read two lines from STDIN, turn it into a string, strip off the
+// trailing newline (and any leading or trailing whitespace),
+// and then split in on spaces. Store the result into an arrays A and B.
+//
+let fs = require ("fs");
+let lines = fs . readFileSync (0) . toString () . split ("\n");
+let A = lines [0] . trim () . split (" ");
+let B = lines [1] . trim () . split (" ");
+
+
+//
+// Iterate over the array B, and print the slices of A.
+//
+for (let i = 0; i < B . length; i ++) {
+ let index = +B [i];
+ console . log (A . slice ( index) . join (" ") + " " +
+ A . slice (0, index) . join (" "));
+}