aboutsummaryrefslogtreecommitdiff
path: root/challenge-271/zapwai/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-271/zapwai/javascript')
-rw-r--r--challenge-271/zapwai/javascript/ch-1.js39
-rw-r--r--challenge-271/zapwai/javascript/ch-2.js54
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-271/zapwai/javascript/ch-1.js b/challenge-271/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..b79e0a378f
--- /dev/null
+++ b/challenge-271/zapwai/javascript/ch-1.js
@@ -0,0 +1,39 @@
+matrix = [ [0, 1],
+ [1, 0],
+ ];
+proc(matrix);
+matrix = [ [0, 0, 0],
+ [1, 0, 1],
+ ];
+proc(matrix);
+matrix = [ [0, 0],
+ [1, 1],
+ [0, 0],
+ ];
+proc(matrix);
+function proc(m) {
+ console.log( "Input: m = ", m);
+ let cnt = [];
+ for (let i = 0; i < m.length; i++) {
+ cnt.push(0);
+ }
+ let pres = 0;
+ for (let row of m) {
+ for (let entry of row) {
+ if (entry == 1) {
+ cnt[pres]++;
+ }
+ }
+ pres++;
+ }
+ let max = 0;
+ let max_index = 0;
+ for (let i = 0; i < cnt.length; i++) {
+ if (cnt[i] > max) {
+ max_index = i;
+ max = cnt[i];
+ }
+ }
+ console.log( "Output: row ", max_index + 1, " ( count is ", max, ")");
+}
+
diff --git a/challenge-271/zapwai/javascript/ch-2.js b/challenge-271/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..7f4dc63a58
--- /dev/null
+++ b/challenge-271/zapwai/javascript/ch-2.js
@@ -0,0 +1,54 @@
+let ints = [0, 1, 2, 3, 4, 5, 6, 7, 8];
+proc(ints);
+ints = [1024, 512, 256, 128, 64];
+proc(ints);
+function proc(ints) {
+ console.log( "Input: ", ints);
+ let count = [];
+ for (let i of ints) {
+ let bin = i.toString(2);
+ let dig = bin.split("");
+ let cnt = 0;
+ for (let d of dig) {
+ if (d == "1") {
+ cnt++;
+ }
+ }
+ count.push(cnt);
+ }
+ let ord = ints;
+ let c = 1;
+ while (c != 0) {
+ c = 0;
+ for (let i = 0; i < ord.length - 1; i++) {
+ if (count[i] > count[i+1]) {
+ c++;
+ let tmp_cnt = count[i];
+ let tmp_int = ord[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ }
+ }
+ }
+ c = 1;
+ while (c != 0) {
+ c = 0;
+ for (let i = 0; i < ord.length - 1; i++) {
+ if (count[i] != count[i+1]) {
+ continue;
+ }
+ if (ord[i] > ord[i+1]) {
+ c++;
+ let tmp_int = ord[i];
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ let tmp_cnt = count[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ }
+ }
+ }
+ console.log( "Output:",ord);
+}