错误代码
报错的代码片段
def merge_sort(items, comp=lambda x, y: x < y):
items_len = len(items)
if items_len < 2:
return items[:]
mid = items_len // 2
left = merge_sort(items[:mid], comp)
right = merge_sort(items[mid:], comp)
return merge(left, right, comp)
def merge(first, second, comp):
result = []
left_index = 0
right_index = 0
while left_index < len(first) and right_index < len(second):
if comp(first[left_index], second(right_index)):
result.append(first[left_index])
left_index += 1
else:
result.append(second[right_index])
right_index += 1
result += first[left_index:]
result += second[right_index:]
return result
错误原因
second参数传入的是一个列表(list), second(right_index)这是一个函数的调用方式, 而列表是不能被调用的,因此抛出一个类型错误.