Reputation: 13
the problem is this https://projecteuler.net/problem=67
I can't understand why this gives a wrong answer- 6580, when it should be 7273.
the idea is to look at the adjacent numbers in the next line and see which is higher and sum that one. So I'm comparing the number in position j with the one in position j+1. If they were equal then it would go in the next line and compare the next 3 numbers, but it doesn't run into 2 equal numbers so I removed the code that dealt with that.
data is a list where in each position has a list with each line of the triangle
data = open('triangulo.txt', 'r')
data = data.readlines()
j = 0
soma = int(data[0].strip())
for i in range(1,100):
if int(data[i].strip().split()[j])==int(data[i].strip().split([j+1]):
print('fodfsf') #this is just to see if there are any numbers equal to each other
if int(data[i].strip().split()[j])>int(data[i].strip().split()[j+1]):
soma = soma + int(data[i].strip().split()[j])
else:
soma = soma + int(data[i].strip().split()[j+1])
j = j + 1
print(soma)
Upvotes: 0
Views: 95
Reputation: 9010
Consider the following triangle.
1
1 2
9 1 1
9 1 1 1
For this triangle, what would your algorithm think is the maximal path?
You are going to need to find a different approach to solving this, but that's half the fun of Project Euler problems.
Upvotes: 1