can_hike_to(List[List[int]], List[int], List[int], int) -> bool The first parameter is an elevation map, m, the second is start cell, s which exists in m, the third is a destination cell, d, which exists in m, and the forth is the amount of available supplies. Under the interpretation that the top of the elevation map is north, you may assume that d is to the north-west of s (this means it could also be directly north, or directly west). The idea is, if a hiker started at s with a given amount of supplies could they reach d if they used the following strategy. The hiker looks at the cell directly to the north and the cell directly to the south, and then travels to the cell with the lower change in elevation. They keep repeating this stratagem until they reach d (return True) or they run out of supplies (return False).
Assume to move from one cell to another takes an amount of supplies equal to the change in elevation between the cells (meaning the absolute value, so cell's being 1 higher or 1 lower would both cost the same amount of supplies). If the change in elevation is the same between going West and going North, the hiker will always go West. Also, the hiker will never choose to travel North, or West of d (they won’t overshoot their destination). That is, if d is directly to the West of them, they will only travel West, and if d is directly North, they will only travel North.
testcases:
def can_hike_to(m: List[List[int]], s: List[int], d: List[int], supplies: int) -> bool:
Examples (note some spacing has been added for human readablity)
map = [[5, 2, 6],
[4, 7, 2],
[3, 2, 1]]
start = [2, 2]
destination = [0, 0]
supplies = 4
can_hike_to(map, start, destination, supplies) == True
start = [1, 2]
destination = [0, 1]
supplies = 5
can_hike_to(map, start, destination, supplies) == False
this is my code:
from typing import List
def can_hike_to(arr: List[List[int]], s: List[int], d: List[int], supp: int) -> bool:
startx = s[0]
starty = s[1]
start = arr[startx][starty] # value
endx = d[0]
endy = d[1]
if startx == endx and starty == endy:
return True
if supp == 0:
return False
else:
try:
north = arr[startx-1][starty] # value
north_exists = True
except IndexError:
north = None
north_exists = False
try:
west = arr[startx][starty-1] # value
west_exists = True
except IndexError:
west = None
west_exists = False
# get change in elevation
if north_exists:
north_diff = abs(north - start)
if west_exists:
west_diff = abs(west - start)
if west_diff <= north_diff:
new_x = startx
new_y = starty - 1
supp -= west_diff
return can_hike_to(arr, [new_x, new_y], d, supp)
elif north_diff < west_diff:
new_x = startx - 1
new_y = starty
supp -= north_diff
return can_hike_to(arr, [new_x, new_y], d, supp)
# if north doesn't exist
elif not north_exists:
if west_exists:
new_x = startx
new_y = starty - 1
supp -= west_diff
return can_hike_to(arr, [new_x, new_y], d, supp)
if not west_exists:
return False
elif not west_exists:
if north_exists:
new_x = startx - 1
new_y = starty
supp -= north_diff
return can_hike_to(arr, [new_x, new_y], d, supp)
if not north_exists:
return False
print(can_hike_to([[5,2,6],[4,7,2],[3,2,1]],[2,2],[0,0],4)) # True
print(can_hike_to([[5,2,6],[4,7,2],[3,2,1]],[1,2],[0,1],5)) # False
it's supposed to return True False but it's returning True True instead. Could someone please respond fast, my assignment is due in less than 2 hours.