202305741LE6.py

mail@pastecode.io avatar
unknown
python
a month ago
2.8 kB
4
Indexable
Never
# While Check
num_c = 0
numcases = int(input())

# While Loop
while num_c != numcases:
    new_string = ""
    matrixnum = int(input())
    divide = matrixnum
    while matrixnum != 0:
        string = str(input() + " ")
        new_string += string
        matrixnum -= 1
    final = new_string.split()
    fifi = final[:]
    length = len(final)
    newline = []
    row_curr = 0
    i = -1
    for var in fifi:
        i += 1
        if i % divide == 0 and i != 0:
            row_curr += 1
        else:
            pass
        # Case First
        if i == 0:
            newline = list(fifi[divide]) + fifi[1:]
        # Case First Row
        elif i <= divide-1:
            newline = newline[:i] + list(fifi[i-1]) + fifi[(i+1):]
        # Case Right Edge
        elif (i + 1) % divide == 0:
            if i <= length - divide:
                newline = newline[:i] + list(fifi[i-divide]) + fifi[(i+1):]
            else:
                newline = newline[:i] + list(fifi[i-divide])
        # Case Left Edge
        elif i % divide == 0:
            if i < length - divide:
                newline = newline[:i] + list(fifi[i+divide]) + fifi[(i+1):]
            else:
                newline = newline[:i] + list(fifi[i+1]) + fifi[(i+1):]
        # Case Last Row
        elif i > length - divide:
            newline = newline[:i] + list(fifi[i + 1]) + fifi[(i + 1):]
        # Case Middle Rows Left Middle
        elif i == (row_curr * divide) + 1 and row_curr != 0:
            if row_curr == 1:
                newline = newline[:i] + list(fifi[i + 1]) + fifi[(i + 1):]
            else:
                newline = newline[:i] + list(fifi[i - divide]) + fifi[(i + 1):]
        # Case Middle Rows Right Middle
        elif i == ((row_curr + 1) * divide) - 2:
            if row_curr == divide - 2:
                newline = newline[:i] + list(fifi[i - 1]) + list(fifi[(i + 1):])
            else:
                newline = newline[:i] + list(fifi[i + divide]) + fifi[(i + 1):]
        # Case Second Row Middle
        elif row_curr == 1:
            newline = newline[:i] + list(fifi[i + 1]) + fifi[(i + 1):]
        # Case Second to Last Row Middle
        elif row_curr == divide - 2:
            newline = newline[:i] + list(fifi[i - 1]) + fifi[(i + 1):]
        # Case Middlemost
        else:
            newline = newline[:i] + list(fifi[i]) + fifi[(i + 1):]
            pass
    rotated = ' '.join(newline)
    count = -1
    array = list(rotated)
    for j in rotated:
        count += 1
        if count % (2 * divide) == 0 and count != 0:
            array[count-1] = '\n'
        else:
            pass
    matrix = ''.join(array)
    print(matrix)
    num_c += 1
Leave a Comment