Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

task with matrices in python

$
0
0

I was solving algorithm tasks in python and found this task with 2 levels of difficulty.

LEVEL 1

In any decent image editor and in modern messengers, there is a feature to rotate an image by 90 degrees. And your task is to implement this function. We don't need a full-fledged photo editor, we'll focus only on rotating the image by 90 degrees.

We assume that the image is represented by a matrix of integers. You have a matrix of size n X m. You need to output the matrix that will be the rotation of the original by 90 degrees clockwise.

Input formatThe first line contains two natural numbers n and m (1 ≤ n,m ≤ 10^3). The following lines contain the description of the matrix: m non-negative integers in each line, not exceeding 10^18.

Output formatOutput m lines, each containing n elements - this is the matrix rotated by 90 degrees.

Example:

Input2 21 12 3Output2 13 1

Input2 31 2 34 5 6

Output4 15 26 3

Input1 169

Output69

I solved this task like this:

def rotate_90_clockwise(matrix):    n = len(matrix)    m = len(matrix[0])    rotated_matrix = [[0] * n for _ in range(m)]    for i in range(n):        for j in range(m):            rotated_matrix[j][n - 1 - i] = matrix[i][j]    return rotated_matrixn, m = map(int, input().split())matrix = []for _ in range(n):    row = list(map(int, input().split()))    matrix.append(row)rotated_matrix = rotate_90_clockwise(matrix)for row in rotated_matrix:    print(*row)

My code basically passes all the tests, but I'm not sure if it's accurate (I'll be glad for any corrections from anyone)Аfter that, I moved on to level 2 and the problems started here.

LEVEL 2

In the previous task, you were required to output the inverted matrix, but now the task is getting more complicated:

Now it's necessary to perform an in-place rotation, meaning without using additional memory. Instead of outputting the result, you need to output the sequence of operations. One operation consists of swapping two matrix elements.

Given a matrix p and the specified rotation direction - clockwise (R) or counterclockwise (L), output the sequence of operations to rotate the original matrix by 90 degrees in the specified direction.

Note that the order of operations doesn't necessarily have to correspond to the actual rotation, it's only important that after all operations, the matrix is rotated by 90 degrees. The number of operations is also not necessarily minimal but should not exceed 7p - the number of elements in the matrix multiplied by 7.

Input format:The first line contains a natural number p (1 ≤ p ≤ 10^6) and an indication of the rotation direction: the symbol 'R' or 'L'. The next p lines contain the description of the matrix - one non-negative integer, not exceeding 10^18, per line.

Output format:The first line - the number k, the required number of operations. k should not exceed 7p^2. Then, in k lines, pairs of numbers are output - the coordinates (x1, y1) and (x2, y2) of the cells between which the elements of the matrix need to be exchanged.

Note:Please note that the indexing of rows and columns of the matrix starts from 0, not 1.

Example:

Input2 L0 00 1

Output11 1 0 1

Input3 R0 1 01 0 04 3 0

Output31 0 1 20 0 2 01 0 2 1

Input1 L5

Output0

I tried to solve the second level task and this is what I got

def rotate_inplace(matrix, direction):    n = len(matrix)    m = len(matrix[0])    operations = []    def transpose():        for i in range(n):            for j in range(i, m):                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]    def reflect():        for i in range(n):            for j in range(m // 2):                matrix[i][j], matrix[i][m - 1 - j] = matrix[i][m - 1 - j], matrix[i][j]    if direction == 'R':        transpose()        reflect()    elif direction == 'L':        reflect()        transpose()    for i in range(n):        for j in range(m):            if (i < n - 1 or j < m - 1) and (i, j) != (n - 1, m - 1):                if direction == 'R':                    operations.append((j, n - 1 - i, i, j))                elif direction == 'L':                    operations.append((i, j, j, n - 1 - i))    return operationsn, direction = input().split()n = int(n)matrix = []for _ in range(n):    row = list(map(int, input().split()))    matrix.append(row)operations = rotate_inplace(matrix, direction)print(len(operations))for op in operations:    print(*op)

But my solution doesn't pass any tests and I have no idea how to solve this task correctly. And now I've decided to ask for help. Thank you all in advance.

P.S. There is also a condition that the time spent on the code should not exceed 3 seconds.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>