简体   繁体   中英

Why my code doesn't work correctly by sometimes returning true instead of false?

This is my code:

 function almostIncreasingSequence(sequence) { var counter = 0; for (var i = 1; i < sequence.length - 1; i++) { if (sequence[i] <= sequence[i - 1]) { counter += 1; } else if (sequence[i + 1] <= sequence[i - 1]) { counter += 1; } } if (counter <= 1) { return true; } return false; } console.log(almostIncreasingSequence([1, 3, 2, 1])); console.log(almostIncreasingSequence([1, 2, 5, 5, 5])); console.log(almostIncreasingSequence([1, 2, 3, 4, 3, 6])); 

This code's job is to:

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Example

For sequence = [1, 3, 2, 1] , the output should be

almostIncreasingSequence(sequence) = false;

There is no one element in this array that can be removed in order to get a strictly increasing sequence.

For sequence = [1, 3, 2] , the output should be

almostIncreasingSequence(sequence) = true.

You can remove 3 from the array to get the strictly increasing sequence [1, 2] . Alternately, you can remove 2 to get the strictly increasing sequence [1, 3] .

But it doesn't work correctly even after a bunch of edits. Currently, these are the arrays that the code doesn't work correctly on:

  • [1, 3, 2, 1] It returns true instead of false.

  • [1, 2, 5, 5, 5] It returns true instead of false.

  • [1, 2, 3, 4, 3, 6] It returns false instead of true.

I'm editing this code long time ago, every time I add something/edit something, a problem gets fixed but another problem gets messed up and it keeps like that.

EDIT: Please tell me why it doesn't work, don't give me the right code please.

 function increasingSequence(sequence,i){ sequence = Array.prototype.slice.call(sequence); sequence.splice(i,1) var len = sequence.length; for(var i=0;i<len-1;i++){ if(sequence[i]>=sequence[i+1])return false; } return true; } function almostIncreasingSequence(sequence) { var len = sequence.length; for(var i=0;i<len-1;i++){ if(sequence[i]>=sequence[i+1]){ return increasingSequence(sequence,i)||increasingSequence(sequence,i+1) } } return true; } console.log(almostIncreasingSequence([1,2,3])); console.log(almostIncreasingSequence([1,3,2])); console.log(almostIncreasingSequence([1,2,3,3,2])); 

I wanted to make this, from a now deleted post , as the right answer:

function almostIncreasingSequence(sequence) {
    return sequence.map((_, index) => [...sequence.slice(0, index), ...sequence.slice(index + 1)])
        .some(arr => arr.every((value, index) => index === 0 || value > arr[index - 1]));
}

The most performant way is to go through the sequence only once. This code remembers the previous number and checks if the current one is higher. If it is lower or equal, then it marks the higher number as the mistake, so the next number can be higher again.

 function isIncreasingWithMistakes(sequence) { prev = Number.MIN_VALUE; var mistakeCounter = 0; for (var number of sequence) { if (prev < number) { // The good case prev = number } else { // Found a mistake mistakeCounter += 1 prev = Math.min(prev, number) // Eliminate the higher number } // Stop immediately when there are too many mistakes if (1 < mistakeCounter) { return false } } return true } // These should be true console.log(isIncreasingWithMistakes([1, 2, 3, 4, 3, 6])) console.log(isIncreasingWithMistakes([10, 1, 2, 3, 4, 5])) // These should be false console.log(isIncreasingWithMistakes([1, 3, 2, 1])) console.log(isIncreasingWithMistakes([1, 2, 5, 5, 5])) console.log(isIncreasingWithMistakes([1,2,3,4,3,6,1])) 

This seems to work

  function _doAlmostIncreasingSequence(_seq, recheck) { var warning = 0; var _control = _seq[0] - 1; for (var i = 0; i < _seq.length; i++) { var _test = _seq[i]; if (_test <= _control) { if (recheck) { var test1 = _seq.slice(0); var test2 = _seq.slice(0); test1.splice(i, 1); test2.splice(i - 1, 1); return _doAlmostIncreasingSequence(test1) || _doAlmostIncreasingSequence(test2); } return false; } _control = _test; } return true; } function almostIncreasingSequence(_seq) { return _doAlmostIncreasingSequence(_seq, 1); } console.log("TRUE :", almostIncreasingSequence([1, 2, 3, 5, 4, 5, 6])); console.log("TRUE :", almostIncreasingSequence([10, 1, 2, 3, 4, 5, 6])); console.log("TRUE :", almostIncreasingSequence([4, 5, 6, 6, 7, 8])); console.log("FALSE :", almostIncreasingSequence([4, 5, 6, 1, 2, 3])); console.log("TRUE :", almostIncreasingSequence([1, 3, 4, 6, 7, 8, 1, 10, 11, 12])); console.log("FALSE :", almostIncreasingSequence([1, 3, 2, 1])); console.log("FALSE :", almostIncreasingSequence([1, 2, 5, 5, 5])); console.log("TRUE :", almostIncreasingSequence([1, 2, 3, 4, 3, 6])); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM