【问题标题】:How does this Python Hackerrank function work?这个 Python Hackerrank 函数是如何工作的?
【发布时间】:2019-10-17 05:08:30
【问题描述】:

就编程而言,我是一个完全的初学者,我刚刚开始学习我的第一门语言,即 Python。最近,我一直在练习在 Hackerrank 中解决问题,但遇到了一些“对角线差”问题。

这个问题对我来说是全新的,所以我在网上搜索了一些答案,并遇到了有人在 github 中制作的这个功能。


    def diagonalDifference(arr):
        prim =0
        sec=0
        length = len(arr[0])
        i=0                           #what does i=0 even do here?
        for count in range(length):
            prim += arr[count][count] #don't understand what "[count][count]" mean
            sec += arr[count][(length-count-1)] #don't understand this either
        return abs(prim-sec)

【问题讨论】:

  • 您是否尝试过调试它,看看会发生什么以及每个步骤中的变量值是什么?
  • 查看函数采用的输入类型。
  • i 什么都不做,你可以删除它。 arr[x][y] 表示arrx, y 位置的元素(我假设它是一个二维方阵)。
  • 该函数似乎接受了一个复杂的参数arr。理解它的关键是为该参数提供一个或多个示例值。
  • 括号通常用于索引容器。例如,如果容器是嵌套列表lst = [["a", "b"], ["c", "d"]],则lst[0][1] 返回'b'(因为[["a", "b"], ["c", "d"]][0] -> ['a", "b"][1] -> "b")。 arr 建议您有一个数组。请注意,numpy 数组可以是 extended 超出基本索引。关于i,它没有在其他任何地方使用,所以它什么也不做。通常,它在循环外保存一个初始计数。

标签: python


【解决方案1】:

这里是相同的代码,有进一步的解释。基本上这个函数对元素求和 从左上角到右下角的对角线将运行总数存储在 prim 中,并求和 右上角到左下角对角线的元素存储以秒为单位的运行总数。然后 返回差的绝对值。对于数组,索引是:arr[row][column] 从 0 到比数组长度小一。希望对你有帮助

import numpy as np

def diagonalDifference(arr):
    prim = 0
    sec = 0
    length = len(arr[0])
    for i in range(length):
        print("Iteration:", i,
              "UL to BR Diagonal:", arr[i][i],
              "UR to BL Diagonal:", arr[i][(length-i-1)])
        # Get value of arr in the ith row and ith column (i.e. the UL to BR diagonal)
        # Add to the cummulative sum
        prim = prim + arr[i][i]

        # Get the value of arr in the ith row and the (length-i-1)th column
        # Columns traverse right to left (i.e. the UR to BL diagonal)
        sec = sec + arr[i][(length-i-1)]

    print("UL to BR Diagonal Sum:", prim,
          "----",
          "UR to BL Diagonal Sum:", sec)
    # Take the absolute value of the difference between the running totals
    return abs(prim-sec)

a = np.array([[1, 2, 4], [3, 4, 6], [3, 8, 1]])
print(a)
diagonalDifference(a)

输出:

[[1 2 4]
 [3 4 6]
 [3 8 1]]
Iteration: 0 UL to BR Diagonal: 1 UR to BL Diagonal: 4
Iteration: 1 UL to BR Diagonal: 4 UR to BL Diagonal: 4
Iteration: 2 UL to BR Diagonal: 1 UR to BL Diagonal: 3
UL to BR Diagonal Sum: 6 ---- UR to BL Diagonal Sum: 11

【讨论】:

    【解决方案2】:

    首先i这里是不必要的。现在,假设我们有一个方阵:

    arr = 
    [[1, 2, 4],
     [3, 5, 8],
     [6, 2, 1]]
    The indices will be:
    [[(0,0), (0,1), (0,2)],
     [(1,0), (1,1), (1,2)],
     [(2,0), (2,1), (2,2)]]
    

    所以主对角线是[(0,0),(1,1),(2,2)] 而次对角线是:[(0,2),(1,1),(2,0)]

    现在在函数中:

    length = len(arr[0])
    arr[0] is := [1, 2, 4], i.e. the first row,
    so length = 3
    for count in range(length):
    so count will have values: [0, 1, 2]
    Now, for all the iterations:
        arr[count][count] will yield: arr[0][0], arr[1][1] and arr[2][2],
        hence giving the first diagonal.
    And 
        arr[count][(length-count-1)] will yield: arr[0][(3-0-1)], arr[1][(3-1-1)],
        and arr[2][(3-2-1)], i.e arr[0][2], arr[1][1] and arr[2][0],
        which is the second diagonal
    

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多