简体   繁体   中英

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? 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
6
4 3 2 1 3 4
3
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.

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. So there needs to be a break just after answer is set.

The error message you saw is due to the following: Both the second and third if statements are always executed. If d is empty, there will be a range error when the third if statement tries to access the first and last elements. 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.

Finally, print(answer) should be outside, rather than inside, the loop.

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)

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