aboutsummaryrefslogtreecommitdiff
path: root/challenge-260/zapwai/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-260/zapwai/javascript')
-rw-r--r--challenge-260/zapwai/javascript/ch-1.js21
-rw-r--r--challenge-260/zapwai/javascript/ch-2.js40
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-260/zapwai/javascript/ch-1.js b/challenge-260/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..3e4fbc4b42
--- /dev/null
+++ b/challenge-260/zapwai/javascript/ch-1.js
@@ -0,0 +1,21 @@
+let l1 = [1,2,2,1,1,3];
+let l2 = [1,2,3];
+let l3 = [-2,0,1,-2,1,1,0,1,-2,9];
+for (let l of [l1, l2, l3])
+ proc(l);
+
+function proc(l) {
+ console.log("Input:", l);
+ let f = {};
+ for (let i of l)
+ (i in f) ? f[i]++ : f[i] = 1;
+ let freq = [];
+ let output = 1;
+ for (let v of Object.values(f)) {
+ if (freq.includes(v))
+ output = 0;
+ else
+ freq.push(v);
+ }
+ console.log("Output:", output);
+}
diff --git a/challenge-260/zapwai/javascript/ch-2.js b/challenge-260/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..c7078f49fa
--- /dev/null
+++ b/challenge-260/zapwai/javascript/ch-2.js
@@ -0,0 +1,40 @@
+function proc(word) {
+ console.log("Input:", word);
+ let h = {};
+ L(word.length, word.split(''), h);
+ let sorted = Object.keys(h).sort();
+ console.log("Output:", 1 + seek_word(word, sorted));
+}
+
+function L(k, list, h) {
+ if (k == 1)
+ h[list.join('')] = 1;
+ else {
+ L(k-1, list, h);
+ for (let i = 0; i < k-1; i++) {
+ if (k % 2 == 0)
+ swap(i, k-1, list);
+ else
+ swap(0, k-1, list);
+ L(k-1, list, h);
+ }
+ }
+}
+
+function swap(i, j, list) {
+ let tmp = list[i];
+ list[i] = list[j];
+ list[j] = tmp;
+}
+
+function seek_word(word, sorted) {
+ for (let i = 0; i < sorted.length; i++)
+ if (sorted[i] == word)
+ return i;
+
+ return -1;
+}
+
+proc("CAT");
+proc("GOGGLE");
+proc("SECRET");