如果我们要从数组中删除一个元素,我想编写一个函数来检查数组是否是增量的。因此,例如,一个类似于
[1,3,4,2,8]返回true,因为如果删除2,数组将是增量的,即1,3,4,8
为了解决这个问题,我创建了一个遍历数组的函数,检查n是否小于n+1,创建一个包含真值和假值的数组,这取决于我们正在迭代的元素是否满足所述条件。
在n+1小于或等于n的情况下,我有另一个检查n+1是否小于n-1的条件,在这个实例中返回“非常假”。
然后,如果我的新Array指示删除一个元素不会给我留下一个增量值数组,我想返回false。我通过检查是否;)我的新数组包含多个假值ii。)我的新数组只包含‘非常假’的值,如果非常假不是数组iii中的最后一个元素的话。)我的数组包含非常假的和假的。
努力让这个逻辑起作用。我增加了几个测试用例--我很难找到正确的答案。
function solution(sequence) {
let newArr = [];
for(let i = 0; i < sequence.length - 1; i++) {
sequence[i] < sequence[i+1] ? newArr.push(true)
: sequence[i+1] <= sequence[i-1] ? newArr.push('very false')
:newArr.push(false)
}
return (newArr.filter(i => i === false).length) > 1 || ( (newArr.includes('very false')) && (newArr[newArr.length - 1] !== 'very false')) || ( newArr.includes(false) && newArr.includes('very false')) ? false : true;
}
//TEST CASES
let one = [1, 2, 3, 4, 5, 3, 5, 6] //I get this right
let two = [40, 50, 60, 10, 20, 30] //I get this right
let three = [1, 2, 3, 4, 3, 6] //I get this wrong
let four = [1, 4, 10, 4, 2] // I get this wrong
//EXPECTED OUTCOMES
console.log(solution(one)) => false;
console.log(solution(two)) => false;
console.log(solution(three)) => true;
console.log(solution(four)) => false;FYI:这不是为了作业或考试。
发布于 2022-02-03 20:24:08
下一个提供的解决方案使用了一种基于every的方法,在这种方法中,人们可以计算可能的增量数组违反OP的标准/要求的数量。根据是否发生了冲突,我们会将当前值与导致违规的最新值进行比较,或者仅仅比较两个连续的数组项。
一旦超过允许的最大冲突计数,every立即退出/中断其迭代,使用false布尔返回值,该值也将成为下面实现的canBeMadeIncremental函数的返回值;否则,这两个进程的返回值都是true。
function canBeMadeIncremental(arr) {
let violationCount = 0;
let comparisonValue = null;
return arr.every((value, idx, arr) => {
let isAchievable = true;
if (idx >= 1) {
comparisonValue = comparisonValue ?? arr[idx - 1];
if (comparisonValue >= value) {
++violationCount;
} else {
comparisonValue = null;
}
isAchievable = (violationCount <= 1);
}
return isAchievable;
});
}
console.log(
"canBeMadeIncremental([1, 2, 3, 4, 5, 3, 5, 6]) ?..",
canBeMadeIncremental([1, 2, 3, 4, 5, 3, 5, 6]) // false
);
console.log(
"canBeMadeIncremental([40, 50, 60, 10, 20, 30]) ?..",
canBeMadeIncremental([40, 50, 60, 10, 20, 30]) // false
);
console.log(
"canBeMadeIncremental([1, 2, 3, 4, 3, 6]) ?..",
canBeMadeIncremental([1, 2, 3, 4, 3, 6]) // true
);
console.log(
"canBeMadeIncremental([1, 4, 10, 4, 2]) ?..",
canBeMadeIncremental([1, 4, 10, 4, 2]) // false
);.as-console-wrapper { min-height: 100%!important; top: 0; }
上面的代码示例可以很容易地重构成一个更通用的函数,该函数提供关于增量数组是否可能以及如果是,如何实现它的数据(例如,需要删除哪个索引/索引).
function howToMakeIncremental(arr) {
let isAchievable = true;
let comparisonValue = null;
let violationCount = 0;
const violatingIndexList = [];
arr.every((value, idx, arr) => {
if (idx >= 1) {
comparisonValue = comparisonValue ?? arr[idx - 1];
if (comparisonValue >= value) {
violatingIndexList.push(idx);
++violationCount;
} else {
comparisonValue = null;
}
isAchievable = (violationCount <= 1);
}
return isAchievable;
});
const howTo = { isAchievable };
if (isAchievable) {
howTo.violatingIndexList = violatingIndexList;
}
return howTo;
}
console.log(
"howToMakeIncremental([1, 2, 3, 4, 5, 3, 5, 6]) ?..",
howToMakeIncremental([1, 2, 3, 4, 5, 3, 5, 6]) // { isAchievable: false }
);
console.log(
"howToMakeIncremental([40, 50, 60, 10, 20, 30]) ?..",
howToMakeIncremental([40, 50, 60, 10, 20, 30]) // { isAchievable: false }
);
console.log(
"howToMakeIncremental([1, 2, 3, 4, 3, 6]) ?..",
howToMakeIncremental([1, 2, 3, 4, 3, 6]) // { isAchievable: true, violatingIndexList: [4] }
);
console.log(
"howToMakeIncremental([1, 4, 10, 4, 2]) ?..",
howToMakeIncremental([1, 4, 10, 4, 2]) // { isAchievable: false }
);.as-console-wrapper { min-height: 100%!important; top: 0; }
https://stackoverflow.com/questions/70976394
复制相似问题