diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-19 20:19:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-19 20:19:07 +0000 |
| commit | da08e8b1a60981a59ded04da47d0eb025a52083d (patch) | |
| tree | 081d3b441571dcdc7e1ed66132f03a39c090435b | |
| parent | a5a72532d7a1fa425e667af7dff9f77f29f07a4d (diff) | |
| parent | 6c473b933e6e87044d694fa8b559bec8eec5b0e9 (diff) | |
| download | perlweeklychallenge-club-da08e8b1a60981a59ded04da47d0eb025a52083d.tar.gz perlweeklychallenge-club-da08e8b1a60981a59ded04da47d0eb025a52083d.tar.bz2 perlweeklychallenge-club-da08e8b1a60981a59ded04da47d0eb025a52083d.zip | |
Merge pull request #7758 from ealvar3z/challenge-208_eax
add week 208 solutions in JS
| -rw-r--r-- | challenge-208/ealvar3z/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-208/ealvar3z/js/ch-1.js | 37 | ||||
| -rw-r--r-- | challenge-208/ealvar3z/js/ch-2.js | 48 |
3 files changed, 86 insertions, 0 deletions
diff --git a/challenge-208/ealvar3z/blog.txt b/challenge-208/ealvar3z/blog.txt new file mode 100644 index 0000000000..436396a6f6 --- /dev/null +++ b/challenge-208/ealvar3z/blog.txt @@ -0,0 +1 @@ +https://eax.bearblog.dev/gang-of-misfits-and-their-arithmetic-progressions/ diff --git a/challenge-208/ealvar3z/js/ch-1.js b/challenge-208/ealvar3z/js/ch-1.js new file mode 100644 index 0000000000..8caf69de9f --- /dev/null +++ b/challenge-208/ealvar3z/js/ch-1.js @@ -0,0 +1,37 @@ +#!/usr/bin/env node + +const assert = require('assert'); + +// Implementation +function solve(a, b) { + const common = a.filter(e => b.includes(e)); + + const minimum_sum = common.reduce((acc, e) => { + const i = a.indexOf(e); + const j = b.indexOf(e); + const sum = i + j; + + if (sum < acc.min_sum) { + return { elements: [e], min_sum: sum }; + } else if (sum == acc.min_sum) { + return { elements: [...acc.elements, e], min_sum: sum }; + } else { + return acc; + } + }, { elements: [], min_sum: Infinity }); + + return minimum_sum.elements; +} + +// Tests +const p = ["Perl", "Raku", "Love"]; +const q = ["Raku", "Perl", "Hate"]; +const r = solve(p, q); +assert.deepStrictEqual(r.sort(), ["Perl", "Raku"]); + +const P = ["A", "B", "C"]; +const Q = ["D", "E", "F"]; +const R = solve(P, Q); +assert.deepStrictEqual(R, []); + +console.log('All tests passed'); diff --git a/challenge-208/ealvar3z/js/ch-2.js b/challenge-208/ealvar3z/js/ch-2.js new file mode 100644 index 0000000000..3b915a4157 --- /dev/null +++ b/challenge-208/ealvar3z/js/ch-2.js @@ -0,0 +1,48 @@ +#!/usr/bin/env node + +// Implementation +function solve(misfits) { + const n = misfits.length; + + // elements + const elements = misfits.reduce((acc, val) => acc + val, 0); + const sum_of_elements = (n * (n + 1)) / 2; + const element_diff = sum_of_elements - elements; + + // squares + const squares = misfits.reduce((acc, val) => acc + val * val, 0); + const sum_of_squares = (n * (n + 1) * (2 * n + 1)) / 6; + const square_diff = sum_of_squares - squares; + + + if (element_diff === 0 && square_diff === 0) { + return -1; + } else { + const missing = (element_diff * element_diff + square_diff) / (2 * element_diff); + const duplicate = missing - element_diff; + return { duplicate, missing }; + } + +} + +// Tests +const assert = require('assert'); + +const a = [1, 2, 3, 4]; +const test_a = solve(a); +assert.deepStrictEqual(test_a, -1); + +const b = [1, 2, 3, 3]; +const test_b = solve(b); +assert.deepStrictEqual(test_b, { duplicate: 3, missing: 4 }); + +const c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +const test_c = solve(c); +assert.deepStrictEqual(test_c, -1); + +const d = [1, 2, 2, 4]; +const test_d = solve(d); +assert.deepStrictEqual(test_d, { duplicate: 2, missing: 3}); + + +console.log('All tests passed'); |
