2018-10-10 18:03:03 -04:00
---
title: Combinations
id: 5958469238c0d8d2632f46db
challengeType: 5
2019-08-28 16:26:13 +03:00
forumTopicId: 302233
2018-10-10 18:03:03 -04:00
localeTitle: Комбинации
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
Задача: < p > Учитывая неотрицательные целые числа < big > m< / big > и < big > n< / big > , генерируйте все размерные < big > m< / big > < a href = "http://mathworld.wolfram.com/Combination.html" title = "ссылка: http://mathworld.wolfram.com/Combination.html" > комбинаций< / a > целых чисел от < big > 0< / big > (ноль) до < big > n-1< / big > в отсортированном порядке (каждая комбинация сортируется и вся таблица сортируется). < / p > Пример: < p > < big > 3< / big > гребня < big > 5< / big > : < / p > < pre > 0 1 2
2018-10-10 18:03:03 -04:00
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 2 3
1 2 4
1 3 4
2 3 4
2019-08-28 16:26:13 +03:00
< / pre >
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
2018-10-10 18:03:03 -04:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > combinations</ code > is a function.
testString: assert(typeof combinations === 'function');
- text: < code > combinations(3, 5)</ code > should return < code > [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]</ code > .
testString: assert.deepEqual(combinations(testInput1[0], testInput1[1]), testOutput1);
- text: < code > combinations(4, 6)</ code > should return < code > [[0,1,2,3], [0,1,2,4], [0,1,2,5], [0,1,3,4], [0,1,3,5], [0,1,4,5], [0,2,3,4], [0,2,3,5], [0,2,4,5], [0,3,4,5], [1,2,3,4], [1,2,3,5], [1,2,4,5], [1,3,4,5], [2,3,4,5]]</ code >
testString: assert.deepEqual(combinations(testInput2[0], testInput2[1]), testOutput2);
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-08-28 16:26:13 +03:00
function combinations(m, n) {
2018-10-10 18:03:03 -04:00
// Good luck!
return true;
}
```
< / div >
2019-08-28 16:26:13 +03:00
### After Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-teardown' >
```js
2019-08-28 16:26:13 +03:00
const testInput1 = [3, 5];
const testOutput1 = [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]];
const testInput2 = [4, 6];
const testOutput2 = [[0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 2, 5], [0, 1, 3, 4], [0, 1, 3, 5], [0, 1, 4, 5], [0, 2, 3, 4], [0, 2, 3, 5], [0, 2, 4, 5], [0, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]];
2018-10-10 18:03:03 -04:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function combinations(m, n) {
const nArr = [...Array(n).keys()];
return (function generateCombinations (size, numArr) {
const ret = [];
for (let i = 0; i < numArr.length ; i + + ) {
if (size === 1) {
ret.push([numArr[i]]);
}
else {
const sub = generateCombinations(size - 1, numArr.slice(i + 1, numArr.length));
for (let subI = 0; subI < sub.length ; subI + + ) {
const next = sub[subI];
next.unshift(numArr[i]);
ret.push(next);
}
}
}
return ret;
}(m, nArr));
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >