So, I have been working on this homework problem for several hours, I will do my best to explain it.
I need to write a program in python that takes a list and launches you in the first element in the list, you can either move to one place or jump over an item and land on the other hand, each element that you land on the expenses number at that place. 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])
, . [0,1,2,1000,0], 3, 1 2, 2 1000, 0. 3, - 2, 0.
, , , , , , .
EDIT: , , . . , , , , . [0, 98, 7, 44, 25, 3, 5, 85, 46, 4] , 87, 124. :
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])