以下两个python函数似乎有什么区别?
我是数据结构和算法的新手,所以当我编写一个函数以从两个数组(一个来自每个数组中的一个)中找到一对数字时,我有点困惑,两个数字的绝对值是最小的。我的初稿是版本2,但没有通过所有测试。但是,当我分配arrayone [i]
为firstNum
和arraytwo [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在版本 1 中,您为第一个 if、elif、else 语句索引 arrayOne 和 arrayTwo,这很好,但是您更改了 i/j 的值,因此如果程序命中 if current
if current
算法的最小部分,i/j 的值发生了变化。
例如,假设
i
和j
,其中 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
andj
where both 0 andarrayOne[0] < arraytwo[0]
, you would incrementi
and continue on. Once you get to the bottom and you want to delclaresmallestPair
, you are now setting the value to[arrayOne[1], arrayTwo[0]
rather than[arrayOne[0], arrayTwo[0]
because you incrementedi
.Version one declares the two variables and uses those declarations for the WHOLE function.