【发布时间】:2021-09-06 17:34:07
【问题描述】:
我写下了这段代码,它可以工作,但是当我将它转换为 .py 并尝试使用输入运行它时,
我会写下递归已达到限制或类似的东西。
注意:我没有使用 sort()、min() 和其他函数,作为我作业的一部分。
def sortl(lst, i, b_ord):
if len(lst) == i:
return lst
elif b_ord > ord(lst[i]):
lst[i - 1] = lst[i]
lst[i] = chr(b_ord)
b_ord = ord(lst[0])
i = 0
else:
b_ord = ord(lst[i])
sortl(lst, (i + 1), b_ord)
lst = list(input())
n = ord('0')
i = 0
sortl(lst,i,n)
print("".join(lst))
【问题讨论】:
-
你没有返回排序递归调用
-
您对
sortl的递归调用每次都使用完整列表,因此每次调用的长度都不会减少,这意味着您有无限递归。
标签: python sorting recursion unicode