我和朋友偶尔会为彼此提出一些代码挑战,以帮助保持敏锐。这一次他提出了这个,我认为这将是很好的做法。在javascript中执行此操作,我已经为它构建了一个Web界面,但是生成所有可能的结果序列非常困难。
我已经通过创建函数来完成它,以便将嵌套循环的一个元素组合,两个元素组合,三个元素组合,4个元素组合和5个元素组合分开工作。
虽然上述想法确实有效,但只要添加第6个输入就会中断。那么有没有办法为N输入实现相同的功能?我一直试图找到一种递归方式来做到这一点,但到目前为止,我已经在这些尝试中遇到了困难。无论这是一个很好的学习机会,我都会喜欢一些帮助!
Input
Your script must prompt the user for the number of fruit, 1 to 5.
The fruit available to the script for each input selection are as follows:
1 = [Cherry, Lime, Banna]
2 = [Cherry, Lime, Banna, Orange, Apple]
3 = [Cherry]
4 = [Cherry, Lime]
5 = [Cherry, Lime, Banna, Orange]
Output
Your script should generate the list of all possible fruit sequences that could be
generated for the given input selection from 1 to 5.
Restrictions
1) The script is allowed to use any fruit only once per sequence, or not at all.
2) The script must generate at least one fruit per sequence.
Sample Input: 4
Sample Output:
[Cherry]
[Lime]
[Cherry][Lime]
[Lime][Cherry]
在没有任何现有库(如Jquery,ES6语法)的情况下执行此操作。
recursiveGenerator = function() {
fruitSequences = [];
fruitRemaining = inputs[selectedInput].slice();
arrayBuilder([], fruitRemaining);
console.log('finshed calculating, resulting fruit sequences');
console.log(fruitSequences);
}
arrayBuilder = function(array, fruitRemaining) {
console.log('called arrayBuilder');
console.log(array, fruitRemaining);
fruitRemaining.forEach( function(fruit) {
if (array.indexOf(fruit) === -1) {
array.push(fruit);
} else {
return;
}
if (array.length < fruitRemaining.length) {
arrayBuilder(array, fruitRemaining);
} else {
fruitSequences.push(array);
fruitRemaining.pop();
}
})
}
答案 0 :(得分:1)
有助于可视化算法。给定列表a b c
,这些是我们想要生成的组合:
单个字符组合:
a b c
1
a b c
1
a b c
1
两个字符组合:
a b c
1 2
a b c
1 2
a b c
1 2
a b c
2 1
a b c
2 1
a b c
2 1
三种角色组合:
a b c
1 2 3
a b c
1 3 2
etc.
可以将其描述为“1到n”组合,其中n是列表的长度。
代码中的一种方法是使用以下一对函数:
let nCombinations = function(list, numItems) {
// Requesting more items than the list isn't possible.
if (list.length < numItems) {
return [];
}
// If requesting 1 item per combination, quickly return a list
// of single item lists.
if (numItems == 1) {
let result = [];
for (let i = 0; i < list.length; i++) {
result.push([list[i]]);
}
return result;
}
// Loop through the list, combining each element with all the
// permutations of the remaining elements.
let result = [];
for (let i = 0; i < list.length; i++) {
let currentElement = list.slice(i, i + 1);
let restList = list.slice(0, i).concat(list.slice(i + 1));
let restCombinations = nCombinations(restList, numItems - 1);
for (let j = 0; j < restCombinations.length; j++) {
result.push(
currentElement.concat(restCombinations[j]));
}
}
return result;
}
let combinations = function(list) {
let result = [];
// Loop through the "1 to n" combinations
for (let i = 1; i <= list.length; i++) {
i_combinations = nCombinations(list, i);
for (let j = 0; j < i_combinations.length; j++) {
result.push(i_combinations[j]);
}
}
return result;
}
将它们用于:
const OPTIONS = [
['Cherry', 'Lime', 'Banana'],
['Cherry', 'Lime', 'Banana', 'Orange', 'Apple'],
['Cherry'],
['Cherry', 'Lime'],
['Cherry', 'Lime', 'Banana', 'Orange']
];
for (var i = 0; i < OPTIONS.length; i++) {
console.log('Option ' + (i + 1) + ': ' + OPTIONS[i]);
console.log(combinations(OPTIONS[i]));
}
产生以下输出:
w$ node select-fruits.js
Option 1: Cherry,Lime,Banana
[ [ 'Cherry' ],
[ 'Lime' ],
[ 'Banana' ],
[ 'Cherry', 'Lime' ],
[ 'Cherry', 'Banana' ],
[ 'Lime', 'Cherry' ],
[ 'Lime', 'Banana' ],
[ 'Banana', 'Cherry' ],
[ 'Banana', 'Lime' ],
[ 'Cherry', 'Lime', 'Banana' ],
[ 'Cherry', 'Banana', 'Lime' ],
[ 'Lime', 'Cherry', 'Banana' ],
[ 'Lime', 'Banana', 'Cherry' ],
[ 'Banana', 'Cherry', 'Lime' ],
[ 'Banana', 'Lime', 'Cherry' ] ]
Option 2: Cherry,Lime,Banana,Orange,Apple
[ [ 'Cherry' ],
[ 'Lime' ],
[ 'Banana' ],
[ 'Orange' ],
[ 'Apple' ],
[ 'Cherry', 'Lime' ],
[ 'Cherry', 'Banana' ],
[ 'Cherry', 'Orange' ],
[ 'Cherry', 'Apple' ],
[ 'Lime', 'Cherry' ],
[ 'Lime', 'Banana' ],
[ 'Lime', 'Orange' ],
[ 'Lime', 'Apple' ],
[ 'Banana', 'Cherry' ],
[ 'Banana', 'Lime' ],
[ 'Banana', 'Orange' ],
[ 'Banana', 'Apple' ],
[ 'Orange', 'Cherry' ],
[ 'Orange', 'Lime' ],
[ 'Orange', 'Banana' ],
[ 'Orange', 'Apple' ],
[ 'Apple', 'Cherry' ],
[ 'Apple', 'Lime' ],
[ 'Apple', 'Banana' ],
[ 'Apple', 'Orange' ],
[ 'Cherry', 'Lime', 'Banana' ],
[ 'Cherry', 'Lime', 'Orange' ],
[ 'Cherry', 'Lime', 'Apple' ],
[ 'Cherry', 'Banana', 'Lime' ],
[ 'Cherry', 'Banana', 'Orange' ],
[ 'Cherry', 'Banana', 'Apple' ],
[ 'Cherry', 'Orange', 'Lime' ],
[ 'Cherry', 'Orange', 'Banana' ],
[ 'Cherry', 'Orange', 'Apple' ],
[ 'Cherry', 'Apple', 'Lime' ],
[ 'Cherry', 'Apple', 'Banana' ],
[ 'Cherry', 'Apple', 'Orange' ],
[ 'Lime', 'Cherry', 'Banana' ],
[ 'Lime', 'Cherry', 'Orange' ],
[ 'Lime', 'Cherry', 'Apple' ],
[ 'Lime', 'Banana', 'Cherry' ],
[ 'Lime', 'Banana', 'Orange' ],
[ 'Lime', 'Banana', 'Apple' ],
[ 'Lime', 'Orange', 'Cherry' ],
[ 'Lime', 'Orange', 'Banana' ],
[ 'Lime', 'Orange', 'Apple' ],
[ 'Lime', 'Apple', 'Cherry' ],
[ 'Lime', 'Apple', 'Banana' ],
[ 'Lime', 'Apple', 'Orange' ],
[ 'Banana', 'Cherry', 'Lime' ],
[ 'Banana', 'Cherry', 'Orange' ],
[ 'Banana', 'Cherry', 'Apple' ],
[ 'Banana', 'Lime', 'Cherry' ],
[ 'Banana', 'Lime', 'Orange' ],
[ 'Banana', 'Lime', 'Apple' ],
[ 'Banana', 'Orange', 'Cherry' ],
[ 'Banana', 'Orange', 'Lime' ],
[ 'Banana', 'Orange', 'Apple' ],
[ 'Banana', 'Apple', 'Cherry' ],
[ 'Banana', 'Apple', 'Lime' ],
[ 'Banana', 'Apple', 'Orange' ],
[ 'Orange', 'Cherry', 'Lime' ],
[ 'Orange', 'Cherry', 'Banana' ],
[ 'Orange', 'Cherry', 'Apple' ],
[ 'Orange', 'Lime', 'Cherry' ],
[ 'Orange', 'Lime', 'Banana' ],
[ 'Orange', 'Lime', 'Apple' ],
[ 'Orange', 'Banana', 'Cherry' ],
[ 'Orange', 'Banana', 'Lime' ],
[ 'Orange', 'Banana', 'Apple' ],
[ 'Orange', 'Apple', 'Cherry' ],
[ 'Orange', 'Apple', 'Lime' ],
[ 'Orange', 'Apple', 'Banana' ],
[ 'Apple', 'Cherry', 'Lime' ],
[ 'Apple', 'Cherry', 'Banana' ],
[ 'Apple', 'Cherry', 'Orange' ],
[ 'Apple', 'Lime', 'Cherry' ],
[ 'Apple', 'Lime', 'Banana' ],
[ 'Apple', 'Lime', 'Orange' ],
[ 'Apple', 'Banana', 'Cherry' ],
[ 'Apple', 'Banana', 'Lime' ],
[ 'Apple', 'Banana', 'Orange' ],
[ 'Apple', 'Orange', 'Cherry' ],
[ 'Apple', 'Orange', 'Lime' ],
[ 'Apple', 'Orange', 'Banana' ],
[ 'Cherry', 'Lime', 'Banana', 'Orange' ],
[ 'Cherry', 'Lime', 'Banana', 'Apple' ],
[ 'Cherry', 'Lime', 'Orange', 'Banana' ],
[ 'Cherry', 'Lime', 'Orange', 'Apple' ],
[ 'Cherry', 'Lime', 'Apple', 'Banana' ],
[ 'Cherry', 'Lime', 'Apple', 'Orange' ],
[ 'Cherry', 'Banana', 'Lime', 'Orange' ],
[ 'Cherry', 'Banana', 'Lime', 'Apple' ],
[ 'Cherry', 'Banana', 'Orange', 'Lime' ],
[ 'Cherry', 'Banana', 'Orange', 'Apple' ],
[ 'Cherry', 'Banana', 'Apple', 'Lime' ],
[ 'Cherry', 'Banana', 'Apple', 'Orange' ],
[ 'Cherry', 'Orange', 'Lime', 'Banana' ],
[ 'Cherry', 'Orange', 'Lime', 'Apple' ],
[ 'Cherry', 'Orange', 'Banana', 'Lime' ],
... 225 more items ]
Option 3: Cherry
[ [ 'Cherry' ] ]
Option 4: Cherry,Lime
[ [ 'Cherry' ],
[ 'Lime' ],
[ 'Cherry', 'Lime' ],
[ 'Lime', 'Cherry' ] ]
Option 5: Cherry,Lime,Banana,Orange
[ [ 'Cherry' ],
[ 'Lime' ],
[ 'Banana' ],
[ 'Orange' ],
[ 'Cherry', 'Lime' ],
[ 'Cherry', 'Banana' ],
[ 'Cherry', 'Orange' ],
[ 'Lime', 'Cherry' ],
[ 'Lime', 'Banana' ],
[ 'Lime', 'Orange' ],
[ 'Banana', 'Cherry' ],
[ 'Banana', 'Lime' ],
[ 'Banana', 'Orange' ],
[ 'Orange', 'Cherry' ],
[ 'Orange', 'Lime' ],
[ 'Orange', 'Banana' ],
[ 'Cherry', 'Lime', 'Banana' ],
[ 'Cherry', 'Lime', 'Orange' ],
[ 'Cherry', 'Banana', 'Lime' ],
[ 'Cherry', 'Banana', 'Orange' ],
[ 'Cherry', 'Orange', 'Lime' ],
[ 'Cherry', 'Orange', 'Banana' ],
[ 'Lime', 'Cherry', 'Banana' ],
[ 'Lime', 'Cherry', 'Orange' ],
[ 'Lime', 'Banana', 'Cherry' ],
[ 'Lime', 'Banana', 'Orange' ],
[ 'Lime', 'Orange', 'Cherry' ],
[ 'Lime', 'Orange', 'Banana' ],
[ 'Banana', 'Cherry', 'Lime' ],
[ 'Banana', 'Cherry', 'Orange' ],
[ 'Banana', 'Lime', 'Cherry' ],
[ 'Banana', 'Lime', 'Orange' ],
[ 'Banana', 'Orange', 'Cherry' ],
[ 'Banana', 'Orange', 'Lime' ],
[ 'Orange', 'Cherry', 'Lime' ],
[ 'Orange', 'Cherry', 'Banana' ],
[ 'Orange', 'Lime', 'Cherry' ],
[ 'Orange', 'Lime', 'Banana' ],
[ 'Orange', 'Banana', 'Cherry' ],
[ 'Orange', 'Banana', 'Lime' ],
[ 'Cherry', 'Lime', 'Banana', 'Orange' ],
[ 'Cherry', 'Lime', 'Orange', 'Banana' ],
[ 'Cherry', 'Banana', 'Lime', 'Orange' ],
[ 'Cherry', 'Banana', 'Orange', 'Lime' ],
[ 'Cherry', 'Orange', 'Lime', 'Banana' ],
[ 'Cherry', 'Orange', 'Banana', 'Lime' ],
[ 'Lime', 'Cherry', 'Banana', 'Orange' ],
[ 'Lime', 'Cherry', 'Orange', 'Banana' ],
[ 'Lime', 'Banana', 'Cherry', 'Orange' ],
[ 'Lime', 'Banana', 'Orange', 'Cherry' ],
[ 'Lime', 'Orange', 'Cherry', 'Banana' ],
[ 'Lime', 'Orange', 'Banana', 'Cherry' ],
[ 'Banana', 'Cherry', 'Lime', 'Orange' ],
[ 'Banana', 'Cherry', 'Orange', 'Lime' ],
[ 'Banana', 'Lime', 'Cherry', 'Orange' ],
[ 'Banana', 'Lime', 'Orange', 'Cherry' ],
[ 'Banana', 'Orange', 'Cherry', 'Lime' ],
[ 'Banana', 'Orange', 'Lime', 'Cherry' ],
[ 'Orange', 'Cherry', 'Lime', 'Banana' ],
[ 'Orange', 'Cherry', 'Banana', 'Lime' ],
[ 'Orange', 'Lime', 'Cherry', 'Banana' ],
[ 'Orange', 'Lime', 'Banana', 'Cherry' ],
[ 'Orange', 'Banana', 'Cherry', 'Lime' ],
[ 'Orange', 'Banana', 'Lime', 'Cherry' ] ]