← 返回 meta 的题目列表Simulate Linux 'cd' Command
类型:online_judge
meta
Implement a function to simulate the 'cd' command in the Linux operating system. The function should support the following operations:
Change to an absolute path, e.g., change from "/" to "/a/b/c"
Change to a relative path, such as if the current path is "/a/b", input "../c" should change to "/a/c"
Handle both ".." and "." in paths, where ".." means move up to the parent directory and "." means the current directory
Function signature:
def change_directory(current_path: str, target_path: str) -> str:
Input Parameters:
current_path (string): The current directory path.
target_path (string): The target path to change to.
Output:
A string representing the full path or an error message if the path is invalid.
Example:
Input: current_path = "/a/b/c", target_path = "../d"
Output: "/a/b/d"
Input: current_path = "/a/b/", target_path = "../../e"
Output: "/a/e"
Input: current_path = "/", target_path = "../"
Output: "Error: Cannot go above root"
Constraints:
Both current_path and target_path should start with "/".
Invalid path suffixes are not allowed.
Example
Input
/a/b/c
../d