Change Directory

Asked at OpenAI· Screen

Reported by the problem's creator, not verified

Import another question

Simulate the Unix cd command. Implement resolve_path(cwd, command), which returns the absolute path of the directory you end up in after running cd command from cwd.

Paths use / as a separator. Component names are non-empty strings with no /. cwd is an absolute, canonical path: it starts with / and contains no ., .., or empty components.

The result always starts with /, has no ., .. or empty components, and has no trailing slash except for the root itself, which is /.

Examples

  1. Example 1

    Input: cwd = /foo/bar, command = boo/./../doe

    Output: /foo/bar/doe

    Explanation: Append boo, . stays in /foo/bar/boo, .. goes back up to /foo/bar, then append doe.

  2. Example 2

    Input: cwd = /a/b, command = /x//y/

    Output: /x/y

    Explanation: The command starts with /, so it is absolute and cwd /a/b is ignored. The empty component from the double slash and the trailing slash are dropped, leaving /x/y.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.