Delete a Directory Tree

Asked at Datadog· Screen

Reported by the problem's creator, not verified

Import another question

You work through delete_tree(entries, undeletable, path), which builds a file system object fs from entries (strings like "D /a" for a directory and "F /a/b.txt" for a file) and undeletable (paths whose delete is always refused). All paths are absolute and start with /. The object exposes exactly three calls:

  • fs.list(path) returns the absolute paths of the direct children of the directory path. For a file it returns an empty list.
  • fs.delete(path) deletes a file or an empty directory and returns True. It returns False and changes nothing for a directory that still has children, and for an entry the system refuses to remove.
  • fs.is_directory(path) returns True for a directory and False for a file.

The starter already ends with return [deleted, fs.remaining()], where fs.remaining() lists the entries still present, sorted as plain strings in the same "D /p" / "F /p" format as entries.

Examples

  1. Example 1

    Input: entries = ["D /a","D /a/b","F /a/b/c.txt","F /a/d.txt","D /x"], undeletable = [], path = /a

    Output: [true,["D /x"]]

    Explanation: The files under /a are removed first, then the emptied /a/b, then /a itself. /x lies outside the subtree and stays.

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.