2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 59f40b17e79dbf1ab720ed7a
|
2020-11-27 19:02:05 +01:00
|
|
|
title: Department Numbers
|
2018-09-30 23:01:58 +01:00
|
|
|
challengeType: 5
|
2019-08-05 09:17:33 -07:00
|
|
|
forumTopicId: 302249
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: department-numbers
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
|
|
|
|
2019-03-01 17:10:50 +09:00
|
|
|
There is a highly organized city that has decided to assign a number to each of their departments:
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2019-03-01 17:10:50 +09:00
|
|
|
<ul>
|
|
|
|
<li>Police department</li>
|
|
|
|
<li>Sanitation department</li>
|
|
|
|
<li>Fire department</li>
|
|
|
|
</ul>
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
|
|
Each department can have a number between 1 and 7 (inclusive).
|
|
|
|
|
2019-03-01 17:10:50 +09:00
|
|
|
The three department numbers are to be unique (different from each other) and must add up to the number 12.
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2019-03-01 17:10:50 +09:00
|
|
|
The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --instructions--
|
|
|
|
|
2019-05-23 13:57:59 +09:00
|
|
|
Write a program which outputs all valid combinations as an array.
|
|
|
|
|
|
|
|
```js
|
|
|
|
[2, 3, 7] [2, 4, 6] [2, 6, 4]
|
|
|
|
[2, 7, 3] [4, 1, 7] [4, 2, 6]
|
|
|
|
[4, 3, 5] [4, 5, 3] [4, 6, 2]
|
|
|
|
[4, 7, 1] [6, 1, 5] [6, 2, 4]
|
2019-03-30 23:06:46 +09:00
|
|
|
[6, 4, 2] [6, 5, 1]
|
2019-05-23 13:57:59 +09:00
|
|
|
```
|
2019-07-19 11:04:45 +02:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`combinations` should be a function.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(typeof combinations === 'function');
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`combinations([1, 2, 3], 6)` should return an Array.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(Array.isArray(combinations([1, 2, 3], 6)));
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`combinations([1, 2, 3, 4, 5, 6, 7], 12)` should return an array of length 14.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
2020-11-27 19:02:05 +01:00
|
|
|
assert(combinations(nums, total).length === len);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`combinations([1, 2, 3, 4, 5, 6, 7], 12)` should return all valid combinations.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert.deepEqual(combinations(nums, total), result);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --after-user-code--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
2018-10-20 21:02:47 +03:00
|
|
|
const nums = [1, 2, 3, 4, 5, 6, 7];
|
|
|
|
const total = 12;
|
|
|
|
const len = 14;
|
|
|
|
const result = [
|
|
|
|
[2, 3, 7],
|
|
|
|
[2, 4, 6],
|
|
|
|
[2, 6, 4],
|
|
|
|
[2, 7, 3],
|
|
|
|
[4, 1, 7],
|
|
|
|
[4, 2, 6],
|
|
|
|
[4, 3, 5],
|
|
|
|
[4, 5, 3],
|
|
|
|
[4, 6, 2],
|
|
|
|
[4, 7, 1],
|
|
|
|
[6, 1, 5],
|
|
|
|
[6, 2, 4],
|
|
|
|
[6, 4, 2],
|
|
|
|
[6, 5, 1]
|
|
|
|
];
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --seed-contents--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
function combinations(possibleNumbers, total) {
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
2019-03-02 17:42:56 +09:00
|
|
|
function combinations(possibleNumbers, total) {
|
2018-09-30 23:01:58 +01:00
|
|
|
let firstNumber;
|
|
|
|
let secondNumber;
|
|
|
|
let thridNumber;
|
|
|
|
const allCombinations = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < possibleNumbers.length; i += 1) {
|
|
|
|
firstNumber = possibleNumbers[i];
|
|
|
|
|
|
|
|
if (firstNumber % 2 === 0) {
|
|
|
|
for (let j = 0; j < possibleNumbers.length; j += 1) {
|
|
|
|
secondNumber = possibleNumbers[j];
|
|
|
|
|
|
|
|
if (j !== i && firstNumber + secondNumber <= total) {
|
|
|
|
thridNumber = total - firstNumber - secondNumber;
|
|
|
|
|
|
|
|
if (thridNumber !== firstNumber && thridNumber !== secondNumber && possibleNumbers.includes(thridNumber)) {
|
|
|
|
allCombinations.push([firstNumber, secondNumber, thridNumber]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allCombinations;
|
|
|
|
}
|
|
|
|
```
|