Python程式列印矩陣的蛇形圖案
在這篇文章中,我們將學習一個Python程式,用於以蛇形圖案列印矩陣。
假設我們已經得到了一個n x n的矩陣。我們將使用下面提到的方法列印輸入矩陣的蛇形圖案。
使用的方法
以下是完成此任務的各種方法:
使用巢狀for迴圈
使用切片反轉交替行
思路
我們將遍歷矩陣的所有行。對於每一行,我們將檢查它是奇數還是偶數。如果行數是偶數,則從左到右列印矩陣;否則,從右到左列印矩陣。
方法1:使用巢狀for迴圈
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存矩陣的行數。
建立一個變數來儲存矩陣的列數。
建立一個函式printSnakePattern(),透過接受輸入矩陣作為引數來列印蛇形圖案的矩陣。
使用global關鍵字使rows和columns變數成為全域性變數。
使用for迴圈遍歷矩陣的行。
使用if條件語句檢查當前行號是否為偶數。
如果條件為真,則使用另一個巢狀for迴圈遍歷當前行的所有列。
如果當前行是偶數,則從左到右列印矩陣行。
否則,如果當前行是奇數,則從右到左列印矩陣行。
建立一個變數來儲存輸入矩陣並列印給定的矩陣。
透過將輸入矩陣作為引數傳遞給上面定義的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)
輸出
執行上述程式後,將生成以下輸出:
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
方法2:使用切片反轉交替行
切片是一種常見的實踐,程式設計師最常使用它來有效地解決問題。考慮一個Python列表。您必須切片列表才能訪問列表元素的範圍。使用冒號(:),一個簡單的切片運算子,是一種實現此目的的方法。
語法
[start:stop:step]
引數
start - 開始索引
end - 結束索引
step - 步長
示例
下面的程式使用切片列印輸入矩陣的蛇形圖案:
# 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)
輸出
執行後,上述程式將生成以下輸出:
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
結論
在本文中,我們學習瞭如何使用兩種不同的方法以蛇形方式列印給定的矩陣。我們學習瞭如何使用global關鍵字使變數成為全域性變數。我們還學習瞭如何透過反向切片反轉任何可迭代物件,包括列表、元組、字串等。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP