Array If-And Condition Not Working As Planned {Python}
Question:
I have a program where, given a sequence of numbers within an array, the goal is to check whether or not no more than one value can be taken out to make the sequence a strictly increasing sequence. One of my conditions is to check whether the value at the current index is larger than the value before it and the index is greater than zero (I don’t see it necessary to check the first index). The issue here is that for some reason when passed into the program the array [10, 1, 2, 3, 4, 5], this condition seems to evaluate to true at the second index.
Here is the code I’ve written:
remove_count = 0
for i in range(len(sequence)):
if i > 0 and sequence[i] <= sequence[i-1]:
...
The expectation was that this would be entirely skipped over and the program would output True
since taking out 10 would satisfy the goal of the program. Yet, somehow when i = 1
the code actually runs and False
is returned. I just have no clue how it even got past the initial if-statement to get to those next lines of code, since 1 is not less than 10.
Edit: Apologies, this question was a bit childish and I think the conditionals got to my head.
Answers:
As you have said the value of the current index has to be greater than the value of the previous index.
And you wrote in the code: if i > 0 and sequence[i] <= sequence[i-1]:
This translates to whether the value at the previous index is greater or equal to the value at the current index.
I have a program where, given a sequence of numbers within an array, the goal is to check whether or not no more than one value can be taken out to make the sequence a strictly increasing sequence. One of my conditions is to check whether the value at the current index is larger than the value before it and the index is greater than zero (I don’t see it necessary to check the first index). The issue here is that for some reason when passed into the program the array [10, 1, 2, 3, 4, 5], this condition seems to evaluate to true at the second index.
Here is the code I’ve written:
remove_count = 0
for i in range(len(sequence)):
if i > 0 and sequence[i] <= sequence[i-1]:
...
The expectation was that this would be entirely skipped over and the program would output True
since taking out 10 would satisfy the goal of the program. Yet, somehow when i = 1
the code actually runs and False
is returned. I just have no clue how it even got past the initial if-statement to get to those next lines of code, since 1 is not less than 10.
Edit: Apologies, this question was a bit childish and I think the conditionals got to my head.
As you have said the value of the current index has to be greater than the value of the previous index.
And you wrote in the code: if i > 0 and sequence[i] <= sequence[i-1]:
This translates to whether the value at the previous index is greater or equal to the value at the current index.