3 回答

TA贡献1785条经验 获得超4个赞
使用额外的变量?
def minimum(lst, current_min=None):
if not lst:
return current_min
if current_min is None:
current_min = lst[0]
elif lst[0] < current_min:
current_min = lst[0]
return minimum(lst[1:], current_min)

TA贡献1818条经验 获得超8个赞
这是一个非常明确的版本,由于注释和变量名称,它应该易于阅读。
def minimum(lst):
# base case
if len(lst) == 1:
return lst[0]
# get first element and minimum of remaining list
first = lst[0]
rest = lst[1:]
min_of_rest = minimum(rest)
# return the smaller one of those two values
if first < min_of_rest:
return first
else:
return min_of_rest
添加回答
举报