Zig Zag Sequence

  • + 2 comments

    Hi there, my code seems to be right and performed only three changes as asked. However even "Your Output" matching "Expected Output", it is counting as failure and not passing the test. Is it common?

    code:

    def findZigZagSequence(a, n):
        a.sort()
        mid = int((n + 1)/2)
        a[mid-1], a[n-1] = a[n-1], a[mid-1] # first  change
    
        st = mid + 1
        ed = mid #  second change
        while(st < n - 1): # third change
            a[st], a[ed] = a[ed], a[st]
            st = st + 1
            ed = ed + 1
    
        for i in range (n):
            if i == n-1:
                print(a[i])
            else:
                print(a[i], end = ' ')
        return
    
    test_cases = int(input())
    for cs in range (test_cases):
        n = int(input())
        a = list(map(int, input().split()))
        findZigZagSequence(a, n)
    

    Input (stdin) 1 7 1 2 3 4 5 6 7

    Your Output (stdout) 1 2 3 7 6 5 4

    Expected Output 1 2 3 7 6 5 4

    Does anyone have a hint of what may be happening?