Reputation: 29064
I am trying to print out the output of the maximum route each in a separate line.
The code is here:
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)
I tried this:
2
3
1
2 3
4 5 6
3
1
2 3
4 5 6
When i try to output out the value, i get this:
print '\n'.join(output)
TypeError: sequence item 0: expected string, int found
How do change this? Need some guidance...
Upvotes: 0
Views: 7692
Reputation: 142106
@grc is correct, but instead of creating a new string with newlines, you could simply do:
for row in output:
print row
Upvotes: 2
Reputation: 23545
Try this:
print '\n'.join(map(str, output))
Python can only join strings together, so you should convert the ints to strings first. This is what the map(str, ...)
part does.
Upvotes: 4