aboutsummaryrefslogtreecommitdiff
path: root/challenge-244/roger-bell-west/javascript/ch-2.js
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-22 10:47:44 +0000
committerGitHub <noreply@github.com>2023-11-22 10:47:44 +0000
commit42d537d9299104338c90dd8565e89798084fc0c1 (patch)
tree5b3f4abd2ee263629a4e54c4f04e16cf67260ac7 /challenge-244/roger-bell-west/javascript/ch-2.js
parent50ae12ea0633b07b1f1cd033b4608d5d6cab5eea (diff)
parentcff6b7e06388a30f03100c03b8b2d88e0bb92cab (diff)
downloadperlweeklychallenge-club-42d537d9299104338c90dd8565e89798084fc0c1.tar.gz
perlweeklychallenge-club-42d537d9299104338c90dd8565e89798084fc0c1.tar.bz2
perlweeklychallenge-club-42d537d9299104338c90dd8565e89798084fc0c1.zip
Merge pull request #9119 from Firedrake/rogerbw-challenge-244
RogerBW solutions for challenge no. 244
Diffstat (limited to 'challenge-244/roger-bell-west/javascript/ch-2.js')
-rwxr-xr-xchallenge-244/roger-bell-west/javascript/ch-2.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-244/roger-bell-west/javascript/ch-2.js b/challenge-244/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..f0599b7a7e
--- /dev/null
+++ b/challenge-244/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,52 @@
+#! /usr/bin/node
+
+"use strict"
+
+function combinations(arr, k) {
+ let c = [];
+ for (let i = 0; i < k; i++) {
+ c.push(i);
+ }
+ c.push(arr.length);
+ c.push(0);
+ let out = [];
+ while (true) {
+ let inner = [];
+ for (let i = k-1; i >= 0; i--) {
+ inner.push(arr[c[i]]);
+ }
+ out.push(inner);
+ let j = 0;
+ while (c[j] + 1 == c[j + 1]) {
+ c[j] = j;
+ j += 1;
+ }
+ if (j >= k) {
+ break;
+ }
+ c[j] += 1;
+ }
+ return out;
+}
+
+function grouphero(nums0) {
+ let nums = [...nums0];
+ nums.sort(function(a,b) {
+ return b-a;
+ });
+ let sum = 0;
+ for (let l = 1; l <= nums.length; l++) {
+ for (let c of combinations(nums, l)) {
+ const h = c[c.length - 1];
+ sum += h * h * c[0];
+ }
+ }
+ return sum;
+}
+
+if (grouphero([2, 1, 4]) == 141) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");