我正在使用谷歌应用程序脚本,我需要一个函数,该函数基于长度为6(范围值从1到20(的数组的前3个最大值返回一个数组,其中每个元素都是基于这3个值的位置的字符组合的字符串。虽然我已经完成了最初的过程(以一种非常低效的方式(,但在特殊情况下,我遇到了麻烦,因为存在可以生成多个组合的重复值。
结构示例:
outcomes_based_on_position=["A"、"B"、"C"、"D"、"E"、"F"]
其中===>位置0为A;B表示位置1;C表示位置2,依此类推。
array1=[1,6,5,4,2,3];
expected_result_from_the_function:["BCD"]
我遇到的问题:
array2=[1,4,5,4,4,3];
expected_result_from_the_function:["CBD","CDB","CBE","CEB","CDE","CED"]
我已经尝试过了(考虑到与应用程序脚本的相似性,这段代码是javascript控制台中的片段,以便打印结果;如果您在应用程序脚本中尝试过,请将"写入控制台"从"console.log(("更改为";至";Logger.log((":
function fCombination(targetArray){
var arrayResults = [];
var resultStr = "";
var positions = [];
var analysis = [...targetArray];
analysis.sort(descOrder);
for (var i=0; i<=2; i++){
var verifyCase = true;
for (var count=0; count<=5;count++){
if(positions[0]==count || positions[1]==count) {
//The position that was already taken into account is skipped
continue;
}else if ((analysis[i]==targetArray[count]) && (verifyCase)){
switch (count){
case 0: resultStr = resultStr+"A"; break;
case 1: resultStr = resultStr+"B"; break;
case 2: resultStr = resultStr+"C"; break;
case 3: resultStr = resultStr+"D"; break;
case 4: resultStr = resultStr+"E"; break;
case 5: resultStr = resultStr+"F"; break;
}
verifyCase = false;
positions.push(count);
}
}
}
switch (true){
case (analysis[1]==analysis[2]): arrayResults=[resultStr, resultStr[0]+resultStr[2]+resultStr[1]]; break;
case (analysis[0]==analysis[1]): arrayResults=[resultStr, resultStr[1]+resultStr[0]+resultStr[2]]; break;
default: arrayResults=[resultStr];
}
return arrayResults;
}
function descOrder(element1, element2) {
if(element1 > element2)
return -1; //Sort element1 before element2
if(element1 < element2)
return 1; //Sort element1 after element2
return 0; //Don't change the positions of element1 and element2
}
//Test, Examples
var targetArray1 = [1,2,15,14,12,10];
var targetArray2 = [1,2,15,14,14,10];
var targetArray3 = [1,2,15,15,14,13];
var expectedResult1 = fCombination(targetArray1);
var expectedResult2 = fCombination(targetArray2);
var expectedResult3 = fCombination(targetArray3);
console.log(expectedResult1);
console.log(expectedResult2);
console.log(expectedResult3);
//Other posible case = [1,2,15,14,13,13]
//Expected output: ["CDE", "CDF"]
//Other posible case = [1,2,15,14,14,14]
//Expected output: ["CDE", "CDE", "CDF", "CFD","CEF", "CFE"]
如有任何帮助,我们将不胜感激。
您的组合不一致。在[15,15,15,15,15,15]的情况下,您希望每个组合都具有相同的值。但是在你的另一个例子[1,4,5,4,3]中,你想要:
["CBD"、"CDB"、"CBE"、"CEB"、"CDE"one_answers"CED"];
但完整的组合列表是:
["CED"、"CDE"、"ECD"、"EDC",'DCE','DEC','EED','EDE','EED','EDE','DEE','DEE,'d','de',EDD,EDD,'DDE','d','BED','BDE',"EBD"、"EDB"、"DBE"、"DEB","FED"、"FDE"、"EFD"、"EDF","DFE","DEF"];
即使你只想把第一个元素放在第一个位置,这似乎是你的一个隐含要求,那么组合应该是这样的:
["CED"、"CDE"、"CEB",'CBE','CEF','CFE',"CDB"、"CBD"、"CDF","CFD"、"CBF"、"CFB"];
但在标题中,您说您想要所有可能的组合。我不得不假设你的意思是数学意义上的,但这不是你的测试用例所做的。所以阅读你的解释的人不清楚你希望你的算法做什么。
但这里有两个实现,第一个创建每个组合,第二个创建每个结合,但不改变第一个元素的位置。
const A_CHAR_CODE = 65;
let toChar = (num) => String.fromCharCode(A_CHAR_CODE + num);
const getIndices = (sorted, indices, combo) => {
let next = sorted.pop();
let lastIndex = -1;
let lastNumber = combo[lastIndex];
do {
indices.push(next.i);
lastNumber = combo[next.i];
next = sorted.pop();
} while (sorted.length && next.x == lastNumber);
sorted.push(next);
}
let createSorted = (combo) => {
return combo.map((x, i) => ({x, i})).sort((el1, el2) => {
if (el1.x > el2.x) return 1;
if (el1.x < el2.x) return -1;
});
}
// Permutes last 2
function fCombination1(combo) {
let sorted = createSorted(combo);
let indices = [];
for (let i = 0; i < 3; i++)
getIndices(sorted, indices, combo);
// Create combinations
let strings = [];
for (let i = 0; i < indices.length - 2; i++) {
for (let j = i + 1; i < indices.length - 1; i++) {
for (let k = j + 1; i < indices.length; i++) {
let c1 = toChar(indices[i])
let c2 = toChar(indices[j])
let c3 = toChar(indices[k]);
strings.push(
c1 + c2 + c3,
c1 + c3 + c2,
c2 + c1 + c3,
c2 + c3 + c1,
c3 + c2 + c1,
c3 + c1 + c3,
);
}
}
}
return strings;
}
// Permutes all 3
function fCombination2(combo) {
let sorted = createSorted(combo);
let firstIndices = [];
let restIndices = [];
getIndices(sorted, firstIndices, combo);
getIndices(sorted, restIndices, combo);
getIndices(sorted, restIndices, combo);
// Create combinations
let strings = [];
firstIndices.forEach(k => {
let arr = restIndices;
for (var i = 0; i < arr.length - 1; i++) {
for (var j = i+1; j < arr.length; j++) {
let c1 = toChar(k)
let c2 = toChar(arr[i])
let c3 = toChar(arr[j])
strings.push(
c1 + c2 + c3,
c1 + c3 + c2,
);
}
}
});
return strings;
}
// Test
console.log(fCombination1([1, 4, 5, 4, 4, 3]));
console.log(fCombination1([1,2,15,14,12,10]));
console.log(fCombination1([1,2,15,14,14,10]));
console.log(fCombination1([1,2,15,14,13,13]));
console.log(fCombination2([1, 4, 5, 4, 4, 3]));
console.log(fCombination2([1,2,15,14,12,10]));
console.log(fCombination2([1,2,15,14,14,10]));
console.log(fCombination2([1,2,15,14,13,13]));