所以让我们说我这个功能:
squeeze([1,4,7,9], 8)
squeeze([1,4,7,9], 0)
我希望该函数返回一个新列表,其中包含:
[1,4,7,8,9]
[0,1,4,7,9]
我想使用递归来实现这个功能,但我遇到了麻烦
def squeeze(x:list, num:int):
if len(x) == 1:
if num < x[0]:
return [num] + x #if the integer is less than the 1st value, put it in the front
elif x[0] < num < x[2]:
return [x[0]] + [num] + [x[2]] #put it in the list
#insert this number into the correct spot
else:
return squeeze(num, x[0:1]) + squeeze(num, x[1:]) #i don't think this is right
我无法比较列表中的数字并使用递归将其放在正确的位置。