Not usually one to have to do this, but I cannot find the problem with my code. Yes, this is a project Euler problem. Any tips or suggestions help.

My code works by getting the location of each number, excluding the first numbers as it has to be predefined, and either adding one or keeping that value to find the location of the next number. The larger of those two numbers is appended to the final. The code DOES work on the 4 level test triangle provided.

testNums = []
nextNum = row0
nextLocation = 0
final = [75]

for x in range(1,15):

    testNums = []
    nextNum = locals()['row' + str(x)]
    testNums.append(nextNum[nextLocation])
    testNums.append(nextNum[nextLocation+1])

    if nextNum[nextLocation] > nextNum[nextLocation+1]:
        nextLocation = nextLocation
    else:
        nextLocation = nextLocation + 1

    final.append(int(max(testNums)))

print (sum(final))

and the list of rows is defined by:

row1 = '75'
row2 = '95 64'
row3 = '17 47 82'
row4 = '18 35 87 10'
row5 = '20 04 82 47 65'
row6 = '19 01 23 75 03 34'
row7 = '88 02 77 73 07 63 67'
row8 = '99 65 04 28 06 16 70 92'
row9 = '41 41 26 56 83 40 80 70 33'
row10 ='41 48 72 33 47 32 37 16 94 29'
row11 ='53 71 44 65 25 43 91 52 97 51 14'
row12 ='70 11 33 28 77 73 17 78 39 68 17 57'
row13 ='91 71 52 38 17 14 91 43 58 50 27 29 48'
row14 ='63 66 04 68 89 53 67 30 73 16 69 87 40 31'
row15 ='04 62 98 27 23 09 70 98 73 93 38 53 60 04 23'

row0 = row1.split(' ')
row1 = row2.split(' ')
row2 = row3.split(' ')
row3 = row4.split(' ')
row4 = row5.split(' ')
row5 = row6.split(' ')
row6 = row7.split(' ')
row7 = row8.split(' ')
row8 = row9.split(' ')
row9 = row10.split(' ')
row10 = row11.split(' ')
row11 = row12.split(' ')
row12 = row13.split(' ')
row13 = row14.split(' ')
row14 = row15.split(' ')

I misunderstood the question, and therefore had the wrong logic. Sorry

Basically, you decide the path by going through the higher of the two numbers starting with row 2, and consequently ending up with a binary choice for every row, while disregarding further choices in the other paths.

You should check the result of each path and find the maximum sum among the paths.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.