我正在尝试确定输入列表是否为严格递增的列表。此外,如果从列表中仅删除一个元素会导致严格递增的列表,则仍将考虑列表为真。以下是我的代码。它似乎有索引错误,但我不理解为什么。
def almostIncreasingSequence(sequence):
n=len(sequence)
count=0
if n<=2:
return True
for i in range (n-1):
#test if the i-th element is bigger or equal to the elements after it. If it is, remove that element, and add one to count
for j in range (i+1,n):
if sequence[i]>=sequence[j]:
sequence.pop(i)
count+=1
#if there is more than one element that has to be taken out, it's false
if count>1:
return False
return True
sequence[i] > sequence[i+1]
的数量就足够了。 - chepnerfor i in range(n-1):
语句是无效的。 - Alex Hallsequence
是否也要作为副作用变成单调递增序列? - chepnercount < 2
时返回 true;在您的示例中count = 1
,因为sequence[0] > sequence[1]
,但是sequence [1] < sequence[2] < sequence[3] < sequence[4]
。 - chepner