Spiral MatrixWrite a Python program to print spiral matrix for a given matrix.Constraints:1<R<101<C<10Example:23number of elements in the array:6elements of array:-1 2 3 4 5 61 2 3 6 5 4 Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 2 3 1 2 3 4 5 61 2 3 6 5 4
Question
Spiral MatrixWrite a Python program to print spiral matrix for a given matrix.Constraints:1<R<101<C<10Example:23number of elements in the array:6elements of array:-1 2 3 4 5 61 2 3 6 5 4 Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 2 3 1 2 3 4 5 61 2 3 6 5 4
Solution 1
Here is a Python program that prints a spiral matrix for a given matrix:
def spiral_matrix(matrix):
result = []
while matrix:
result += matrix.pop(0)
if matrix and matrix[0]:
for row in matrix:
result.append(row.pop())
if matrix:
result += matrix.pop()[::-1]
if matrix and matrix[0]:
for row in matrix[::-1]:
result.append(row.pop(0))
return result
R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
if 1<R<10 and 1<C<10:
matrix = []
print("Enter the entries rowwise:")
for i in range(R):
a =[]
for j in range(C):
a.append(int(input()))
matrix.append(a)
print(spiral_matrix(matrix))
else:
print("Constraints: 1<R<10 and 1<C<10")
This program first asks for the number of rows and columns. It then asks for the elements of the matrix row by row. The spiral_matrix function is used to convert the matrix into a spiral matrix. The function works by continuously popping the first row of the matrix and appending it to the result. It then appends the last element of each remaining row to the result. It then appends the last row in reverse order to the result. Finally, it appends the first element of each remaining row in reverse order to the result. This process is repeated until the matrix is empty.
Solution 2
Here is a Python program that prints a spiral matrix for a given matrix:
def spiral_matrix(matrix):
result = []
while matrix:
result += matrix.pop(0)
if matrix and matrix[0]:
for row in matrix:
result.append(row.pop())
if matrix:
result += matrix.pop()[::-1]
if matrix and matrix[0]:
for row in matrix[::-1]:
result.append(row.pop(0))
return result
# Test case
matrix = [[1, 2, 3], [6, 5, 4]]
print(spiral_matrix(matrix))
This program works by repeatedly popping the first row of the matrix and appending it to the result. Then it appends the last element of each remaining row to the result. It then appends the last row (in reverse order) to the result, and finally the first element of each remaining row (in reverse order) to the result. This process is repeated until the matrix is empty.
In the test case, the input matrix is [[1, 2, 3], [6, 5, 4]]. The output of the program is [1, 2, 3, 4, 5, 6], which is the spiral order of the matrix.
Similar Questions
Write a Python program to print spiral matrix for a given matrix.
Given a 2D square matrix, print the matrix in a spiral order. Refer to examples for more details. From an interview's point of view, after you scan the matrix in a 2D array, try to print the matrix in a spiral order without using any extra space.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains N - the size of the matrix [NxN]. It is followed by N lines each containing N integers - matrix elements.Output FormatFor each test case, print the matrix in a spiral order, separated by newline.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput41121 24 331 2 38 9 47 6 55-44 25 -52 69 -517 22 51 27 -44-79 28 -78 1 -4765 -77 -14 -21 -6-96 43 -21 -20 90Output11 2 3 41 2 3 4 5 6 7 8 9-44 25 -52 69 -5 -44 -47 -6 90 -20 -21 43 -96 65 -79 17 22 51 27 1 -21 -14 -77 28 -78
iven a 2D square matrix, rotate it by 90 degrees clockwise.Note:Try to solve it by first scanning the matrix, then doing an in-place rotation, and then printing the rotated matrix.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains the N - the size of the matrix [NxN]. It is followed by N lines each containing N integers - matrix elements.Output FormatFor each test case, print the rotated matrix, separated by a new line.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput41121 24 331 2 38 9 47 6 55-44 25 -52 69 -517 22 51 27 -44-79 28 -78 1 -4765 -77 -14 -21 -6-96 43 -21 -20 90OutputTest Case #1:1Test Case #2:4 13 2Test Case #3:7 8 16 9 25 4 3Test Case #4:-96 65 -79 17 -4443 -77 28 22 25-21 -14 -78 51 -52-20 -21 1 27 6990 -6 -47 -44 -5
Create a matrix having diagonal elements as 1 and all other elements as 0 of size (5, 6).Print the Numpy array.
Write a python program that defines a matrix and prints
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.