Skip to content

Commit 3cc976b

Browse files
Sean-Kenneth-Dohertytrekhleb
authored andcommitted
Fix zero-length combinations
1 parent 115e428 commit 3cc976b

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/algorithms/sets/combinations/__test__/combineWithRepetitions.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import factorial from '../../../math/factorial/factorial';
33

44
describe('combineWithRepetitions', () => {
55
it('should combine string with repetitions', () => {
6+
expect(combineWithRepetitions(['A', 'B'], 0)).toEqual([
7+
[],
8+
]);
9+
610
expect(combineWithRepetitions(['A'], 1)).toEqual([
711
['A'],
812
]);
@@ -25,6 +29,8 @@ describe('combineWithRepetitions', () => {
2529
['B', 'B', 'B'],
2630
]);
2731

32+
expect(combineWithRepetitions(['A', 'B'], -1)).toEqual([]);
33+
2834
expect(combineWithRepetitions(['A', 'B', 'C'], 2)).toEqual([
2935
['A', 'A'],
3036
['A', 'B'],

src/algorithms/sets/combinations/__test__/combineWithoutRepetitions.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import pascalTriangle from '../../../math/pascal-triangle/pascalTriangle';
44

55
describe('combineWithoutRepetitions', () => {
66
it('should combine string without repetitions', () => {
7+
expect(combineWithoutRepetitions(['A', 'B'], 0)).toEqual([
8+
[],
9+
]);
10+
711
expect(combineWithoutRepetitions(['A', 'B'], 3)).toEqual([]);
812

913
expect(combineWithoutRepetitions(['A', 'B'], 1)).toEqual([
@@ -29,6 +33,8 @@ describe('combineWithoutRepetitions', () => {
2933
['A', 'B', 'C'],
3034
]);
3135

36+
expect(combineWithoutRepetitions(['A', 'B', 'C'], -1)).toEqual([]);
37+
3238
expect(combineWithoutRepetitions(['A', 'B', 'C', 'D'], 3)).toEqual([
3339
['A', 'B', 'C'],
3440
['A', 'B', 'D'],

src/algorithms/sets/combinations/combineWithRepetitions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
* @return {*[]}
55
*/
66
export default function combineWithRepetitions(comboOptions, comboLength) {
7+
if (comboLength === 0) {
8+
return [[]];
9+
}
10+
11+
if (comboLength < 0) {
12+
return [];
13+
}
14+
715
// If the length of the combination is 1 then each element of the original array
816
// is a combination itself.
917
if (comboLength === 1) {

src/algorithms/sets/combinations/combineWithoutRepetitions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
* @return {*[]}
55
*/
66
export default function combineWithoutRepetitions(comboOptions, comboLength) {
7+
if (comboLength === 0) {
8+
return [[]];
9+
}
10+
11+
if (comboLength < 0) {
12+
return [];
13+
}
14+
715
// If the length of the combination is 1 then each element of the original array
816
// is a combination itself.
917
if (comboLength === 1) {

0 commit comments

Comments
 (0)