Soluție HackerRank pentru Zig Zag Sequence. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Zig Zag Sequence

Cerinta completa

In this challenge, the task is to debug the existing code to successfully execute all provided test files.


Given an array of [Expresie matematică indisponibilă în copia arhivată] distinct integers, transform the array into a zig zag sequence by permuting the array elements. A sequence will be called a zig zag sequence if the first [Expresie matematică indisponibilă în copia arhivată] elements in the sequence are in increasing order and the last [Expresie matematică indisponibilă în copia arhivată] elements are in decreasing order, where [Expresie matematică indisponibilă în copia arhivată]. You need to find the lexicographically smallest zig zag sequence of the given array.

Example.

[Expresie matematică indisponibilă în copia arhivată]

Now if we permute the array as [Expresie matematică indisponibilă în copia arhivată], the result is a zig zag sequence.

Debug the given function findZigZagSequence to return the appropriate zig zag sequence for the given input array.

Note: You can modify at most three lines in the given code. You cannot add or remove lines of code.

To restore the original code, click on the icon to the right of the language selector.

Input Format

The first line contains [Expresie matematică indisponibilă în copia arhivată] the number of test cases. The first line of each test case contains an integer [Expresie matematică indisponibilă în copia arhivată], denoting the number of array elements.
The next line of the test case contains [Expresie matematică indisponibilă în copia arhivată] elements of array [Expresie matematică indisponibilă în copia arhivată].

Constraints

[Expresie matematică indisponibilă în copia arhivată]
[Expresie matematică indisponibilă în copia arhivată] ([Expresie matematică indisponibilă în copia arhivată] is always odd)
[Expresie matematică indisponibilă în copia arhivată]

Output Format

For each test cases, print the elements of the transformed zig zag sequence in a single line.

Sample Input 0

1
7
1 2 3 4 5 6 7

Sample Output 0

1 2 3 7 6 5 4

Limbajul de programare folosit: python3

Cod:

def findZigZagSequence(a, n):
    a.sort()
    mid = int((n - 1) / 2)
    a[mid], a[n-1] = a[n-1], a[mid]

    st = mid + 1
    ed = n - 2
    while(st <= ed):
        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)

Scor obtinut: 1.0

Submission ID: 464615384

Link challenge: https://www.hackerrank.com/challenges/zig-zag-sequence/problem

Zig Zag Sequence