简体   繁体   English

这两个代码有什么区别?

[英]What's the difference between the two of the codes?


Second one shows IndexError: deque index out of range while the other runs perfectly, can someone explain the same?第二个显示 IndexError: deque index out of range 而另一个完美运行,有人可以解释一下吗? The outputs should only be Yes or No.输出只能是“是”或“否”。

from collections import deque
def check(d):
    while d:
        big = d.popleft() if d[0]>d[-1] else d.pop()
        if not d:
            return "Yes"
        if d[-1]>big or d[0]>big:
            return "No"

for i in range(int(input())):
    int(input())
    d = deque(map(int,input().split()))
    print(check(d))
from collections import deque

for i in range(int(input())):
    int(input())
    numbers = list(map(int, input().split()))
    d = deque(numbers)
    n = len(numbers)
    
    while d:
        if d[0]>d[-1]:
            previous = d.popleft()
        else:
            previous = d.pop()
        
        if not d:
            answer = "Yes"
        if d[-1]>previous or d[0]>previous:
            answer = "No"
        
        print(answer)

Sample Input:样本输入:
2 2
6 6
4 3 2 1 3 4 4 3 2 1 3 4
3 3
1 3 2 1 3 2

In your first program:在您的第一个程序中:

while d:
    big = d.popleft() if d[0]>d[-1] else d.pop()
    if not d:
        return "Yes"
    if d[-1]>big or d[0]>big:
        return "No"

The return will exit the loop when either the deque is empty or the first or last elements are larger than the element just popped off.当双端队列为空或第一个或最后一个元素大于刚刚弹出的元素时, return将退出循环。

But in your second program:但是在你的第二个程序中:

while d:
    if d[0]>d[-1]:
        previous = d.popleft()
    else:
        previous = d.pop()
    
    if not d:
        answer = "Yes"
    if d[-1]>previous or d[0]>previous:
        answer = "No"

The loop isn't exited until the deque is empty, but you want it to be exited as soon as you can determine the value for answer , just like your first program does.在双端队列为空之前不会退出循环,但是您希望在确定answer的值后立即退出,就像您的第一个程序一样。 So there needs to be a break just after answer is set.因此,在设置answer后需要break一下。

The error message you saw is due to the following: Both the second and third if statements are always executed.您看到的错误消息是由于以下原因:总是执行第二个和第三个if语句。 If d is empty, there will be a range error when the third if statement tries to access the first and last elements.如果d为空,则当第三个if语句尝试访问第一个和最后一个元素时会出现范围错误。 The fix in the previous paragraph - adding break after answer = "Yes" - will also fix this error, but it wouldn't hurt to make the intent explicit by changing the third if to elif , so that it's only executed if d is nonempty.上一段中的修复 - 在answer = "Yes"之后添加break - 也将修复此错误,但通过将第三个if更改为elif来明确意图并没有什么坏处,因此它仅在d非空时执行.

Finally, print(answer) should be outside, rather than inside, the loop.最后, print(answer)应该在循环之外,而不是在循环内部。

Here's a fixed version of the second program:这是第二个程序的固定版本:

from collections import deque

for i in range(int(input())):
    int(input())
    numbers = list(map(int, input().split()))
    d = deque(numbers)
    n = len(numbers)
    
    while d:
        if d[0]>d[-1]:
            previous = d.popleft()
        else:
            previous = d.pop()
        
        if not d:
            answer = "Yes"
            break
        elif d[-1]>previous or d[0]>previous:
            answer = "No"
            break
    
    print(answer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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