In a file system, every file has an absolute path such as /home/ana/notes/todo.txt: its parts are separated by /, and / alone is the root. A relative path says how to get somewhere from a given directory instead: .. means "go up one directory" and a name means "go down into it".
Write relative_path(start, target) that returns the shortest relative path from directory start to target, both absolute:
- First normalise both paths: empty parts (from
//or a trailing/) and.parts are ignored, and..removes the part before it (at the root,..stays at the root). - The result is made of
..parts, then names, joined with/, and it never ends with/. - If
startandtargetare the same directory, return".".
Examples
Input: start = "/home/ana/projects/site", target = "/home/ana/notes/todo.txt"
Output: "../../notes/todo.txt"
Input: start = "/", target = "/usr/local/bin"
Output: "usr/local/bin"
Input: start = "/data/raw/", target = "/data/./raw//2024/../2025"
Output: "2025"
Explanation: The target normalises to /data/raw/2025.
Input: start = "/a/bc", target = "/a/b"
Output: "../b"
Explanation: "bc" and "b" are different directories, even though one name starts with the other.
Constraints
- Both paths start with
/and have at most10**4parts. - Names contain no
/; a part is only special when it is exactly.or...
Goals
- Normalise a path by splitting it into parts and resolving `.` and `..`
- Find the length of a common prefix of two lists by walking them together with `zip`
- Build the answer from repeated `..` parts and a slice, then join once