In this article, we will learn a Python program to print a matrix in snake pattern.
Assume we have taken the n x n matrix. We will now print the input matrix in a snake pattern using the below-mentioned methods.
The following are the various methods used to accomplish this task −
Use nested for loops
Reversing Alternate Rows Using Slicing
We will iterate through all rows of the matrix. For each row, we will check if it is even or odd. If the rows are even, the matrix is printed from left to right, otherwise the matrix is printed from right to left.
Below are the algorithms/steps to perform the required task. −
Create a variable to store the number of rows of a matrix.
Create another variable to store the number of columns of a matrix.
Create a function printSnakePattern() to print a matrix in snake pattern by accepting an input matrix as a parameter.
Use the global keyword to make the rows and columns variables global.
Use for loop to traverse the rows of the matrix.
Use the if conditional statement to check whether the current line number is an even number.
Use another nested for loop to traverse through all the columns of the current row if the condition is true.
Print the matrix row from left to right if the current row is even.
Otherwise, if the current row is odd, print the matrix rows from right to left.
Create a variable to store the input matrix and print the given matrix.
Call the printSnakePattern() function defined above by passing the input matrix as a parameter.
The following program prints the input matrix in a snake pattern using nested for loops:
# initializing the number of rows of the matrix rows = 4 # initializing the number of columns of the matrix columns = 4 # creating a function for printing the matrix in # snake pattern accepting the input matrix as an argument. def printSnakePattern(inputMatrix): # making the rows and columns variables global global rows, columns # traversing through the rows of a matrix for m in range(rows): # checking whether the current row number is even if m % 2 == 0: # traversing through all the columns of the current row for n in range(columns): # printing from left to right if the current row is even print(inputMatrix[m][n], end=" ") # Else, printing from right to left if the current row is even else: # traversing from the end of the columns for n in range(columns - 1, -1, -1): print(inputMatrix[m][n], end=" ") # input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] print("The Given Matrix is :") print(inputMatrix) # calling the above-defined printSnakePattern function # by passing the input matrix as an argument. print("Snake Pattern of the given Matrix is:") printSnakePattern(inputMatrix)
On executing, the above program will generate the following output −
The Given Matrix is : [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] Snake Pattern of the given Matrix is: 3 4 5 6 80 60 40 10 1 9 7 8 15 14 20 40
slicing is a frequent practice and the one that programmers utilize the most to solve problems effectively. Consider a Python list. You must slice a list to access a range of list elements. The use of the colon(:), a simple slicing operator, is one method for accomplishing this.
[start:stop:step]
start − Index from where to start
end − ending index
Steps − The number of jumps between i, that is, the step length
The following program prints the input matrix in a snake pattern using slicing.
# input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] # initializing the number of rows of a matrix rows = 4 # initializing the number of columns of a matrix columns = 4 # creating a function for printing the matrix in # snake pattern accepting the input matrix as an argument. def printSnakePattern(inputMatrix): # making the rows and columns variables global global rows, columns # traversing through the rows of a matrix for m in range(rows): # checking whether the current row number is even if m % 2 != 0: # Reversing the row using reverse slicing inputMatrix[m] = inputMatrix[m][::-1] # traversing through the rows of a matrix for m in range(rows): # traversing through all the columns of the current row for n in range(columns): # printing the corresponding element print(inputMatrix[m][n], end=' ') # input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] print("The Given Matrix is :") print(inputMatrix) # calling the above-defined printSnakePattern function # by passing the input matrix as an argument. print("Snake Pattern of the given Matrix is:") printSnakePattern(inputMatrix)
On execution, the above program will generate the following output −
The Given Matrix is : [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] The Snake Pattern of the given Matrix is: 3 4 5 6 80 60 40 10 1 9 7 8 15 14 20 40
In this article, we learned how to print the given matrix in snake form using two different methods. We learned how to use the global keyword to make variables global. We also learned how to reverse any iterable, including a list, tuple , string, etc. via reverse slicing.
The above is the detailed content of Python program to print matrix in snake pattern. For more information, please follow other related articles on the PHP Chinese website!