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
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
Solution 1
Here is a Python program that prints a spiral matrix for a given matrix:
def spiral_matrix(matrix):
result = []
while matrix:
# append the first row
result += matrix.pop(0)
if matrix and matrix[0]:
# append the last element of each row
for row in matrix:
result.append(row.pop())
if matrix:
# append the last row in reverse order
result += matrix.pop()[::-1]
if matrix and matrix[0]:
# append the first element of each row in reverse order
for row in matrix[::-1]:
result.append(row.pop(0))
return result
# test the function
matrix = [[1, 2, 3], [4, 5, 6]]
print(spiral_matrix(matrix))
This program defines a function spiral_matrix that takes a 2D list (matrix) as input and returns a list of its elements in spiral order. The function works by repeatedly popping the first row of the matrix, appending the last element of each remaining row, popping the last row (in reverse order), and appending the first element of each remaining row (also in reverse order), until the matrix is empty. The test at the end of the program creates a 2x3 matrix and prints its elements in spiral order.
Solution 2
The problem is asking to print a given matrix in a spiral order. Here is a step-by-step solution in Python:
- First, define the function that will print the matrix in spiral order:
def spiralOrder(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
- Now, let's create a matrix from your example:
matrix = [[-1, 2, 3], [4, 5, 6]]
- Finally, call the function with the matrix as an argument and print the result:
print(spiralOrder(matrix))
This will print: [-1, 2, 3, 6, 5, 4]
This function 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, pops the last row and appends it in reverse order, and finally appends the first element of each remaining row in reverse order. This process is repeated until the matrix is empty.
Solution 3
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 the function
matrix = [[1, 2, 3], [4, 5, 6]]
print(spiral_matrix(matrix))
This program defines a function spiral_matrix that takes a 2D list (matrix) as input and returns a list of its elements in spiral order. The function works by repeatedly taking the first row of the matrix, the last element of each remaining row, the last row in reverse order, and the first element of each remaining row (also in reverse order), until there are no elements left.
In the test, the function is called with a 2x3 matrix. The output should be [1, 2, 3, 6, 5, 4], which is the spiral order of the elements in the matrix.
Solution 4
Here is a Python program that prints a spiral matrix for a given matrix:
def spiral_matrix(matrix):
result = []
while matrix:
# append the first row
result += matrix.pop(0)
if matrix and matrix[0]:
# append the last element of each row
for row in matrix:
result.append(row.pop())
if matrix:
# append the last row in reverse order
result += matrix.pop()[::-1]
if matrix and matrix[0]:
# append the first element of each row in reverse order
for row in matrix[::-1]:
result.append(row.pop(0))
return result
# test the function
matrix = [[1, 2, 3], [4, 5, 6]]
print(spiral_matrix(matrix))
This program defines a function spiral_matrix that takes a 2D list (matrix) as input and returns a list of its elements in spiral order. The function works by repeatedly popping the first row of the matrix, the last element of each remaining row, the last row (in reverse order), and the first element of each remaining row (in reverse order), until the matrix is empty. The popped elements are appended to the result list.
The test at the end of the program creates a 2x3 matrix and prints its elements in spiral order. The output of the program is [1, 2, 3, 6, 5, 4], which is the spiral order of the elements in 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
Write a python program that defines a matrix and prints
Create a matrix having diagonal elements as 1 and all other elements as 0 of size (5, 6).Print the Numpy array.
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
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.