From 60bb10be6e3bdb600773860d2933e715e3a0d19a Mon Sep 17 00:00:00 2001 From: divlo Date: Wed, 8 Jul 2020 19:56:30 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20"pairwise"=20challenge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challenges/pairwise/README.md | 25 +++++++++++++++++++ challenges/pairwise/input-output.json | 22 ++++++++++++++++ challenges/pairwise/solutions/.gitkeep | 0 .../solutions/javascript-pairwise/README.md | 4 +++ .../solutions/javascript-pairwise/solution.js | 15 +++++++++++ 5 files changed, 66 insertions(+) create mode 100644 challenges/pairwise/README.md create mode 100644 challenges/pairwise/input-output.json create mode 100644 challenges/pairwise/solutions/.gitkeep create mode 100644 challenges/pairwise/solutions/javascript-pairwise/README.md create mode 100644 challenges/pairwise/solutions/javascript-pairwise/solution.js diff --git a/challenges/pairwise/README.md b/challenges/pairwise/README.md new file mode 100644 index 0000000..d8265b1 --- /dev/null +++ b/challenges/pairwise/README.md @@ -0,0 +1,25 @@ +# pairwise + +Created by [@Divlo](https://github.com/Divlo) at 8 July 2020. + +## Instructions : + +Given an array `arr`, find element pairs whose sum equal the second argument `arg` and return the sum of their indices. + +You may use multiple pairs that have the same numeric elements but different indices. Each pair should use the lowest possible available indices. Once an element has been used it cannot be reused to pair with another element. For instance, `pairwise([1, 1, 2], 3)` creates a pair `[2, 1]` using the 1 at index 0 rather than the 1 at index 1, because 0+2 < 1+2. + +For example `pairwise([7, 9, 11, 13, 15], 20)` returns `6`. The pairs that sum to 20 are `[7, 13]` and `[9, 11]`. We can then write out the array with their indices and values. + +| Index | 0 | 1 | 2 | 3 | 4 | +|-------|---|---|----|----|----| +| Value | 7 | 9 | 11 | 13 | 15 | + +Below we'll take their corresponding indices and add them. + +- 7 + 13 = 20 → Indices 0 + 3 = 3 +- 9 + 11 = 20 → Indices 1 + 2 = 3 +- 3 + 3 = 6 → Return 6 + +## Examples : + +See the `input-output.json` file for examples of input/output. diff --git a/challenges/pairwise/input-output.json b/challenges/pairwise/input-output.json new file mode 100644 index 0000000..a87ad1d --- /dev/null +++ b/challenges/pairwise/input-output.json @@ -0,0 +1,22 @@ +[ + { + "input": [[1, 4, 2, 3, 0, 5], 7], + "output": 11 + }, + { + "input": [[1, 3, 2, 4], 4], + "output": 1 + }, + { + "input": [[1, 1, 1], 2], + "output": 1 + }, + { + "input": [[0, 0, 0, 0, 1, 1], 1], + "output": 10 + }, + { + "input": [[], 100], + "output": 0 + } +] diff --git a/challenges/pairwise/solutions/.gitkeep b/challenges/pairwise/solutions/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/challenges/pairwise/solutions/javascript-pairwise/README.md b/challenges/pairwise/solutions/javascript-pairwise/README.md new file mode 100644 index 0000000..f8159a3 --- /dev/null +++ b/challenges/pairwise/solutions/javascript-pairwise/README.md @@ -0,0 +1,4 @@ +# javascript-pairwise - pairwise + +Programming language : JavaScript +Created by [@Divlo](https://github.com/Divlo) at 8 July 2020. diff --git a/challenges/pairwise/solutions/javascript-pairwise/solution.js b/challenges/pairwise/solutions/javascript-pairwise/solution.js new file mode 100644 index 0000000..b4902c7 --- /dev/null +++ b/challenges/pairwise/solutions/javascript-pairwise/solution.js @@ -0,0 +1,15 @@ +function solution (arr, arg) { + return arr.reduce((accumulator, currentNumber, indexReduce, array) => { + for (let index = indexReduce + 1; index < array.length; index++) { + if (array[index] + array[indexReduce] === arg) { + accumulator += indexReduce + index + array[indexReduce] = NaN + array[index] = NaN + break + } + } + return accumulator + }, 0) +} + +module.exports = solution