在本文中,我們將學習一個Python程序,以蛇形模式列印矩陣。
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 −
#使用巢狀的for迴圈
#Reversing Alternate Rows Using Slicing
#我們將迭代遍歷矩陣的所有行。對於每一行,我們將檢查它是偶數還是奇數。如果行是偶數,則從左到右列印矩陣,否則從右到左列印矩陣。
以下是執行所需任務的演算法/步驟。 −
Create a variable to store the number of rows of a matrix.
Create another variable to store the number of columns of a matrix.
建立一個函數printSnakePattern(),透過接受輸入矩陣作為參數以蛇形模式列印矩陣。
Use the global keyword to make the rows and columns variables global.
使用for迴圈遍歷矩陣的行。
使用if條件語句來檢查目前行號是否為偶數。
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.
否則,如果目前行是奇數,則從右到左列印矩陣行。
Create a variable to store the input matrix and print the given matrix.
#透過將輸入矩陣當作參數來呼叫上述定義的 printSnakePattern() 函數。
以下程式使用嵌套的for迴圈以蛇形模式列印輸入矩陣:
# 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 − 從哪裡開始的索引
#end − ending index
#步驟 − 在i之間要跳躍的次數,即步長
以下程式使用切片以蛇形模式列印輸入矩陣。
# 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 iterword to make variables global. We also learned how to reverse yterable to make variables global。 , string, etc. via reverse slicing.
以上是Python程式以蛇形模式列印矩陣的詳細內容。更多資訊請關注PHP中文網其他相關文章!