def insert3(x,ss):
left = [] #why this need to add properly by list, or not it just return the recent result.
while ss!= []:
for y in ss:
if x<= ss[0]:
return left + [x] + ss[0:]
else:
ss, left = ss[1:], left + [ss[0]]
return left + ss + [x]
print(insert3(6,[2,4,5,7,8]))
这是函数的for循环的正确用法吗?你知道吗
我改变了一点。是这样吗?你知道吗
def insert3(x,ss):
left = []
for y in ss:
if x<= ss[0]:
return left + [x] + ss[0:]
else:
ss, left = ss[1:], left + [ss[0]]
return left + ss + [x]
print(insert3(6,[2,4,5,7,8]))
使用来自这个question的对分将是解决问题的好方法。另见wikipedia。但一个简单的例子是:
为什么要编写复杂的代码插入到排序列表中?您可以使用以下内容:
除非遇到巨大的性能瓶颈,否则最好只使用该语言的特性。我喜欢称之为开发工作的优化。你知道吗
相关问题 更多 >
编程相关推荐