Python 打印蛇形模式的矩阵

Python 打印蛇形模式的矩阵

在本文中,我们将学习一个Python程序,以蛇形模式打印矩阵。

假设我们已经取得了n x n的矩阵。现在我们将使用下面提到的方法以蛇形模式打印输入矩阵。

使用的方法

完成此任务使用了以下各种方法:

  • 使用嵌套for循环

  • 使用切片反转交替行

直觉

我们将遍历矩阵的所有行。对于每一行,我们现在将检查它是偶数行还是奇数行。如果是偶数行,则从左到右打印矩阵,否则从右到左打印。

方法1:使用嵌套for循环

步骤

以下是执行所需任务的算法/步骤:

  • 创建一个变量以存储矩阵的行数。

  • 创建另一个变量以存储矩阵的列数。

  • 创建一个名为 printSnakePattern() 的函数,通过接受输入矩阵作为参数来以蛇形模式打印矩阵。

  • 使用 global 关键字将行数和列数的变量设置为全局变量。

  • 使用 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

结论

在这篇文章中,我们学习了如何使用两种不同的方法以蛇形形式打印给定的矩阵。我们学习了如何使用全局关键字使变量成为全局变量。我们还学习了如何通过反向切片来反转任何可迭代对象,包括列表、元组、字符串等。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程