使用 Python 檢查二進位制矩陣中的水平和垂直對稱性
二進位制矩陣是一個矩形網格,其中每個元素要麼是 0 要麼是 1,表示真或假狀態。它廣泛用於表示各個學科中的關係、連線性和模式。
假設我們已經獲取了一個包含 N 行 M 列的二維二進位制輸入矩陣。我們將使用以下方法檢查輸入矩陣是否水平或垂直對稱或兩者兼而有之。
如果第一行與最後一行匹配,第二行與倒數第二行匹配,依此類推,則稱該矩陣為水平對稱。
如果第一列與最後一列匹配,第二列與倒數第二列匹配,依此類推,則稱該矩陣為垂直對稱。
示例
1 0 1 0 1 0 1 0 1
水平對稱:當矩陣水平翻轉時,第一行“1 0 1”是第三行“1 0 1”的精確映象反射。第一行中的每個元素都對應於第三行中相同位置的元素。
垂直對稱:當矩陣垂直翻轉時,第一列“1 0 1”是第三列“1 0 1”的映象版本。第一列中的元素與第三列中相同位置的元素對齊。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個函式checkHorizontalVertical(),透過將輸入矩陣、行數和列數作為引數來檢查輸入矩陣是否水平或垂直對稱。
將horizontal_symmetric和vertical_symmetric初始化為True,以假設矩陣在兩個方向上都是對稱的。
比較第一行與最後一行、第二行與倒數第二行,依此類推,以檢查水平對稱性。
遍歷矩陣一半的行。
在迴圈內,遍歷列並比較當前行的每個單元格與正在比較的行中相應單元格。
如果任何單元格不同,則將horizontal_symmetric設定為False並中斷迴圈。
為下一次迭代,將行號加1,並將最後一行中的行號減1。
接下來,透過比較第一列與最後一列、第二列與倒數第二列,依此類推,檢查垂直對稱性。
遍歷矩陣一半的列。
在迴圈內,遍歷行並比較當前列的每個單元格與正在比較的列中相應單元格。
如果任何單元格不同,則將vertical_symmetric設定為False並中斷迴圈。
為下一次迭代,將列號加1,並將最後一列中的列號減1。
檢查對稱條件
如果horizontal_symmetric和vertical_symmetric都不為True,則列印“既不是水平對稱也不是垂直對稱”。
如果horizontal_symmetric為True但vertical_symmetric為False,則列印“輸入矩陣水平對稱”。
如果vertical_symmetric為True但horizontal_symmetric為False,則列印“輸入矩陣垂直對稱”。
如果horizontal_symmetric和vertical_symmetric都為True,則列印“輸入矩陣水平和垂直都對稱”。
使用inputMatrix、行數和列數作為引數呼叫checkHorizontalVertical函式。
示例
以下示例比較了行與其從末尾開始的對應行,以檢查水平對稱性,並比較了列與其從末尾開始的對應列,以檢查垂直對稱性。根據比較結果,它列印輸入矩陣是水平對稱、垂直對稱、水平和垂直都對稱,還是既不是水平對稱也不是垂直對稱。
# Creating a function to check whether the input matrix is
# horizontal or vertically symmetric by passing the input matrix,
# no of rows and columns as arguments
def checkHorizontalVertical(inputMatrix, rows, cols):
# Initializing both the horizontal and vertical
# symmetric as true at first
horizontal_symmetric = True
vertical_symmetric = True
# We compare the first row with the last row, the second row with second
# last row and so on.
p = 0
x = rows - 1
# Traversing till half of the rows of the matrix
while p < rows // 2:
# Traversing in the columns
for q in range(cols):
# Checking if each cell is the same or not
if inputMatrix[p][q] != inputMatrix[x][q]:
# If it is not the same then that horizontal symmetric is false
horizontal_symmetric = False
break
# Incrementing the row number by 1
p += 1
x -= 1
# Checking for Vertical Symmetry. The first column is compared to the last column,
# the second column to the second-to-last column, and so forth.
p = 0
x = cols - 1
# Traversing till half of the columns
while p < cols // 2:
# Checking each row of the column
for q in range(rows):
# Checking if each cell is the same or not
if inputMatrix[p][q] != inputMatrix[x][q]:
# If it is not the same then that vertical symmetric is false
vertical = False
break
# Incrementing the column number by 1
p += 1
x -= 1
# checking whether not horizontal and not vertically symmetric
if not horizontal_symmetric and not vertical_symmetric:
# printing Neither horizontal nor vertical symmetric if the condition is true
print("Neither horizontal nor vertical symmetric")
# checking whether the matrix is horizontally symmetric but not vertical
elif horizontal_symmetric and not vertical_symmetric:
# printing horizontal symmetric matrix
print("The input matrix is horizontal symmetric")
# checking whether the matrix is vertically symmetric but not horizontal
elif vertical_symmetric and not horizontal_symmetric:
# printing vertically symmetric
print("The input matrix is vertical symmetric")
else:
# else printing both horizontal and vertical symmetric
print("The input matrix is both horizontal and vertically symmetric")
# input matrix
inputMatrix = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
# calling the above checkHorizontalVertical() by passing
# input matrix, no of rows, columns as arguments
checkHorizontalVertical(inputMatrix, 3, 3)
輸出
The input matrix is both horizontal and vertically symmetric
結論
我們在本文中介紹瞭如何使用巢狀for迴圈遍歷矩陣,以及如何使用for迴圈遍歷每列的行。最後,我們學習瞭如何確定特定矩陣是水平對稱還是垂直對稱。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP