这个Python Hackerrank函数是如何工作的?

这个Python Hackerrank函数是如何工作的?,python,Python,就编程而言,我是一个完全的初学者,我刚刚开始学习我的第一门语言,即Python。最近,我一直在练习在Hackerrank中解决问题,我被困在一些“对角差”问题上 这个问题对我来说是全新的,所以我在网上搜索一些答案,发现了一些人在github中开发的功能 def diagonalDifference(arr): prim =0 sec=0 length = len(arr[0]) i=0

就编程而言,我是一个完全的初学者,我刚刚开始学习我的第一门语言,即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 = 
[[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

下面是经过进一步解释的相同代码。基本上,这个函数对元素求和 左上角到右下角的对角线,以prim形式存储运行总数,以及总和 右上角到左下角对角线上的元素,以秒为单位存储运行总数。然后 返回差值的绝对值。对于数组,索引为:arr[行][列] 从0到小于数组长度的1。希望能有帮助

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

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