aboutsummaryrefslogtreecommitdiff
path: root/challenge-260/zapwai/javascript
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2024-03-16 20:07:25 -0400
committerGitHub <noreply@github.com>2024-03-16 20:07:25 -0400
commitff5e8ece15a2384fbfb530710f10aa485017d4a5 (patch)
tree6fe268d9ceb2b25a2951ea984c077450feb2efae /challenge-260/zapwai/javascript
parent60f1003122fbada697317d943c238593f86db579 (diff)
parent62e7fc3bb85a74125663f4fbd0a5911f6f30c81f (diff)
downloadperlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.tar.gz
perlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.tar.bz2
perlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.zip
Merge branch 'manwar:master' into work
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");