给定一个未排序整数列表和一个目标整数,找出列表中任何对的差值是否与递归的目标整数相等。
>>> aList = [5, 4, 8, -3, 6]
>>> target = 9
return True
>>> aList = [-1, 5, 4]
>>> target = 3
return False
Run Code Online (Sandbox Code Playgroud)
我试过这个,但没有用。
def calculate(aList, target):
if len(aList) == 0 and diff != 0:
return False
startIndex = 0
endIndex = len(aList) - 1
return resursive_sum(aList, target, startIndex, endIndex)
def resursive_sum(aList, targ, start, end):
print(f'Start: {start}')
print(f'End: {end}')
if start == end:
return False
elif aList[end] - aList[start] == targ:
return True
elif aList[end] - aList[start] < targ:
return resursive_sum(values, targ, start, end - 1)
return resursive_sum(aList, targ, start + 1, end)
Run Code Online (Sandbox Code Playgroud)
如果我们不能使用循环对列表进行排序,我不确定如何解决这个问题。即使我们可以使用递归对列表进行排序,递归看起来应该如何才能扫描每一对的差异?
归档时间: |
|
查看次数: |
108 次 |
最近记录: |