我正在尝试使用mergesort算法,当我输出一些文本时,它最终会被搞砸。这是我的代码:
def merge(left, right):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
def mergesort(lst):
if len(lst) <= 1:
return lst
middle = int(len(lst) / 2)
left = mergesort(lst[:middle])
right = mergesort(lst[middle:])
return merge(left, right)
a = [3,1,2,5,9,6,7]
print 'Input #1: ' + str(a)
print 'Output #1: ' + mergesort(a)
我收到此语法错误:
File "Lab1.py", line 21
print 'Input #1: ' + str(a)
^
SyntaxError: invalid syntax
我正在使用 Python 3.3.2 。