So I've been working on this homework problem for a few hours, I'll do my best to explain it.
I need to write a program in python that takes a list and starts you at the first item in the list, you can either move forward one space or jump over an item and land on the other side of it, each item you land on costs the number on that location. The goal is to get to the end as cheaply as possible.
I wrote this function,
def player(cost, board, player_pos):
if player_pos == (len(board)) - 1:
return cost
if player_pos < (len(board)) - 2:
if board[player_pos + 1] > board[player_pos + 2]:
return player(cost + board[player_pos + 2], board, player_pos + 2)
else:
return player(cost + board[player_pos + 1], board, player_pos + 1)
elif player_pos == (len(board)) - 2:
return (cost + board[player_pos] + board[player_pos + 1])
but it can't see past the next two positions, so it might make mistakes. A good example is this list [0,1,2,1000,0] my program outputs 3 because it picks 1 over 2, then 2 over a 1000, then 0. This adds up to 3, but the fastest way is jump to 2, then to 0.
In the homework it says it might take a long time to run long lists, I'm guessing they want me to try every possible combination of jumps and pick the cheapest one but I don't see how to do that with using recursion.
EDIT: So this is the improvement I made based off of the comments, It works with all the examples my prof. gave me except for one, this is the list it doesn't return what he said it should. [0, 98, 7, 44, 25, 3, 5, 85, 46, 4] He says it should return 87, but my adjusted program returns 124. Here's the new code:
def player(cost, board, player_pos):
if player_pos == (len(board)) - 1:
return cost
if player_pos < (len(board)) - 2:
if (player(cost + board[player_pos + 2], board, player_pos + 2)) < (player(cost + board[player_pos + 1], board, player_pos + 1)):
return player(cost + board[player_pos + 2], board, player_pos + 2)
else: return player(cost + board[player_pos + 1], board, player_pos + 1)
elif player_pos == (len(board)) - 2:
return (cost + board[player_pos] + board[player_pos + 1])