I'm trying to create a simple game of "Jump It", it's supposed to take the numbers in a line (input obtained from an file), and find the cheapest route to get to the end. It takes the numbers in positions 1 & 2 and compares them to find which one is smaller, then adds that to the total "cost", then goes on to the next two.
So for the line 0 3 80 6 57 10, the cheapest route would be 3+6+10 for a total of 19. The current error I am getting is "index out of range", how would I fix this error?
cost = 
for line in A:
if (A > A):
elif(A < A):
# This opens the file in read mode
filename = open('input.dat', 'r')
# Read in all the lines of the file into a list of lines
linesList = filename.readlines()
rows = [[int(n) for n in row.split()] for row in linesList]
myData = [[int(val) for val in line.split()] for line in linesList[1:]]
Here is a simple recursive code written in
Python 2.7. Have a look:
def shouldJump(A,i,n): if i>n-1: return 0 elif i+1>n-1: return A[i] else: if A[i]<A[i+1]: return A[i] + shouldJump(A,i+2,n) else: return A[i+1] + shouldJump(A,i+2,n) A = [[0,3,80,6,57,10],[0,1,5,7,2]] cost =  for line in A: cost.append(shouldJump(line,1,len(line))) print cost
Hope it helps!!!