curriculum/challenges/english/blocks/daily-coding-challenges-python/698a1a863194f1f4e63f645f.md
Given an array of strings representing your trail map, return a string of the moves needed to get to your goal.
The given strings will contain the values:
"C": Your current location"G": Your goal"T": Traversable parts of the trail"-": Untraversable parts of the mapReturn a string with the moves needed to follow the trail from your location to your goal where:
"R" is a move right
"D" is a move down
"L" is a move left
"U" is a move up
There will always be a single continuous trail, without any branching, from your current location to your goal.
Each trail location will have a maximum of two traversable locations touching it.
For example, given:
[
"-CT--",
"--T--",
"--TT-",
"---T-",
"---G-"
]
Return "RDDRDD".
navigate_trail(["-CT--", "--T--", "--TT-", "---T-", "---G-"]) should return "RDDRDD".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(navigate_trail(["-CT--", "--T--", "--TT-", "---T-", "---G-"]), "RDDRDD")`)
}})
navigate_trail(["-----", "--TTG", "--T--", "--T--", "CTT--"]) should return "RRUUURR".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(navigate_trail(["-----", "--TTG", "--T--", "--T--", "CTT--"]), "RRUUURR")`)
}})
navigate_trail(["-C----", "TT----", "T-----", "TTTTT-", "----G-"]) should return "DLDDRRRRD".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(navigate_trail(["-C----", "TT----", "T-----", "TTTTT-", "----G-"]), "DLDDRRRRD")`)
}})
navigate_trail(["--------", "-CTTT---", "----T---", "---GT---", "--------"]) should return "RRRDDL".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(navigate_trail(["--------", "-CTTT---", "----T---", "---GT---", "--------"]), "RRRDDL")`)
}})
navigate_trail(["TTTTTTT-", "T-----T-", "T-----T-", "TTTT--TG", "---C----"]) should return "ULLLUUURRRRRRDDDR".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(navigate_trail(["TTTTTTT-", "T-----T-", "T-----T-", "TTTT--TG", "---C----"]), "ULLLUUURRRRRRDDDR")`)
}})
def navigate_trail(map):
return map
def navigate_trail(map):
rows = len(map)
cols = len(map[0])
directions = [
(0, 1, "R"),
(1, 0, "D"),
(0, -1, "L"),
(-1, 0, "U"),
]
for r in range(rows):
for c in range(cols):
if map[r][c] == "C":
row, col = r, c
result = ""
prev = None
while map[row][col] != "G":
for dr, dc, move in directions:
new_row = row + dr
new_col = col + dc
if (
0 <= new_row < rows and
0 <= new_col < cols and
(map[new_row][new_col] == "T" or
map[new_row][new_col] == "G") and
(not prev or (new_row, new_col) != prev)
):
result += move
prev = (row, col)
row, col = new_row, new_col
break
return result