我正在尝试在单独的行中打印出最大路线的输出。
代码在这里:
def triangle(rows):
PrintingList = list()
for rownum in range (rows ):
PrintingList.append([])
newValues = map(int, raw_input().strip().split())
PrintingList[rownum] += newValues
return PrintingList
def routes(rows,current_row=0,start=0):
for i,num in enumerate(rows[current_row]):
if abs(i-start) > 1:
continue
if current_row == len(rows) - 1:
yield [num]
else:
for child in routes(rows,current_row+1,i):
yield [num] + child
testcases = int(raw_input())
output = []
for num in range(testcases):
rows= int(raw_input())
triangleinput = triangle(rows)
max_route = max(routes(triangleinput),key=sum)
output.append(sum(max_route))
print '\n'.join(output)
我试过这个:
2
3
1
2 3
4 5 6
3
1
2 3
4 5 6
当我尝试输出值时,我得到了这个:
print '\n'.join(output)
TypeError: sequence item 0: expected string, int found
如何改变这个?需要一些指导...