だから私はこの関数を言ってみましょう:
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
リスト内の数値を比較し、再帰を使用して正しい場所に配置するのに問題があります。