有一个高度组织化的城市决定为每个部门分配一个号码:
警察局环卫部门消防部门每个部门的数字可以在1到7之间(含)。
这三个部门编号应该是唯一的(彼此不同),并且必须加起来为12。
警察局长不喜欢奇怪的号码,并希望他的部门有一个偶数。
任务:编写一个输出所有有效组合的程序:
[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]
combinations
应该是一个功能。
testString: 'assert(typeof combinations === "function", "combinations
should be a function.");'
- text: 'combinations([1, 2, 3], 6)
应该返回一个数组。'
testString: 'assert(Array.isArray(combinations([1, 2, 3], 6)), "combinations([1, 2, 3], 6)
should return an Array.");'
- text: 'combinations([1, 2, 3, 4, 5, 6, 7], 12)
应返回长度为14的数组。'
testString: 'assert(combinations(nums, total).length === len, "combinations([1, 2, 3, 4, 5, 6, 7], 12)
should return an array of length 14.");'
- text: 'combinations([1, 2, 3, 4, 5, 6, 7], 12)
应返回所有有效组合。'
testString: 'assert.deepEqual(combinations(nums, total), result, "combinations([1, 2, 3, 4, 5, 6, 7], 12)
should return all valid combinations.");'
```