以下两个python函数似乎有什么区别?

发布于 2025-01-19 19:48:23 字数 1681 浏览 0 评论 0原文

我是数据结构和算法的新手,所以当我编写一个函数以从两个数组(一个来自每个数组中的一个)中找到一对数字时,我有点困惑,两个数字的绝对值是最小的。我的初稿是版本2,但没有通过所有测试。但是,当我分配arrayone [i]firstNumarraytwo [j] secondnum,它传递了测试用例。谁能解释到底发生了什么?因为我认为这两个版本完全相同。

版本1:

def smallestDifference(arrayOne, arrayTwo):
    arrayOne.sort()
    arrayTwo.sort()
    i = j = 0
    smallest = float('inf')
    current = float('inf')
    smallestPair = []

    while i < len(arrayOne) and j < len(arrayTwo):
        firstNum = arrayOne[i]
        secondNum = arrayTwo[j]
        if firstNum < secondNum:
            current = abs(firstNum - secondNum)
            i += 1
        elif firstNum > secondNum:
            current = abs(firstNum - secondNum)
            j += 1
        else:
            return [firstNum, secondNum]
        if current < smallest:
            smallest = current
            smallestPair = [firstNum, secondNum]
    return smallestPair

版本2:

def smallestDifference(arrayOne, arrayTwo):
    arrayOne.sort()
    arrayTwo.sort()
    i = j = 0
    smallest = float('inf')
    current = float('inf')
    smallestPair = []

    while i < len(arrayOne) and j < len(arrayTwo):
        if arrayOne[i] < arrayTwo[j]:
            current = abs(arrayOne[i] - arrayTwo[j])
            i += 1
        elif arrayOne[i] > arrayTwo[j]:
            current = abs(arrayOne[i] - arrayTwo[j])
            j += 1
        else:
            return [arrayOne[i], arrayTwo[j]]
        if current < smallest:
            smallest = current
            smallestPair = [arrayOne[i], arrayTwo[j]]
    return smallestPair

I'm new to data structures and algorithms, so I was kind of confused when I was writing a function to find a pair of numbers from two arrays (one from each array) that the absolute value of difference of the two numbers is the smallest. My first draft was version 2, but it did not pass all the tests. However, when I assigned arrayOne[i] to firstNum and arrayTwo[j] to secondNum, it passed all the test cases. Could anyone explain what happened exactly? Since I thought these two versions were exactly the same.

Version 1:

def smallestDifference(arrayOne, arrayTwo):
    arrayOne.sort()
    arrayTwo.sort()
    i = j = 0
    smallest = float('inf')
    current = float('inf')
    smallestPair = []

    while i < len(arrayOne) and j < len(arrayTwo):
        firstNum = arrayOne[i]
        secondNum = arrayTwo[j]
        if firstNum < secondNum:
            current = abs(firstNum - secondNum)
            i += 1
        elif firstNum > secondNum:
            current = abs(firstNum - secondNum)
            j += 1
        else:
            return [firstNum, secondNum]
        if current < smallest:
            smallest = current
            smallestPair = [firstNum, secondNum]
    return smallestPair

Version 2:

def smallestDifference(arrayOne, arrayTwo):
    arrayOne.sort()
    arrayTwo.sort()
    i = j = 0
    smallest = float('inf')
    current = float('inf')
    smallestPair = []

    while i < len(arrayOne) and j < len(arrayTwo):
        if arrayOne[i] < arrayTwo[j]:
            current = abs(arrayOne[i] - arrayTwo[j])
            i += 1
        elif arrayOne[i] > arrayTwo[j]:
            current = abs(arrayOne[i] - arrayTwo[j])
            j += 1
        else:
            return [arrayOne[i], arrayTwo[j]]
        if current < smallest:
            smallest = current
            smallestPair = [arrayOne[i], arrayTwo[j]]
    return smallestPair

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2025-01-26 19:48:23

在版本 1 中,您为第一个 if、elif、else 语句索引 arrayOne 和 arrayTwo,这很好,但是您更改了 i/j 的值,因此如果程序命中 if current if current 算法的最小部分,i/j 的值发生了变化。

例如,假设 ij ,其中 0 和 arrayOne[0] arrayOne[0] arraytwo[0],您将增加 i 并继续。一旦您到达底部并且想要声明 smallestPair,您现在将值设置为 [arrayOne[1], arrayTwo[0] 而不是 [ arrayOne[0]、arrayTwo[0] 因为您增加了 i

第一个版本声明了两个变量并将这些声明用于整个函数。

In version 1, you are indexing arrayOne and arrayTwo for your first if,elif,else statement which is fine, however you change the value of i/j so if the program hits the if current < smallest part of the algorithm, the value of i/j has changed.

For example lets say i and j where both 0 and arrayOne[0] < arraytwo[0], you would increment i and continue on. Once you get to the bottom and you want to delclare smallestPair, you are now setting the value to [arrayOne[1], arrayTwo[0] rather than [arrayOne[0], arrayTwo[0] because you incremented i.

Version one declares the two variables and uses those declarations for the WHOLE function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文