Delete a Directory Tree
Asked at Datadog· Screen
Reported by the problem's creator, not verified
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 directorypath. For a file it returns an empty list.fs.delete(path)deletes a file or an empty directory and returnsTrue. It returnsFalseand changes nothing for a directory that still has children, and for an entry the system refuses to remove.fs.is_directory(path)returnsTruefor a directory andFalsefor 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
Example 1
Input: entries =
["D /a","D /a/b","F /a/b/c.txt","F /a/d.txt","D /x"], undeletable =[], path =/aOutput:
[true,["D /x"]]Explanation: The files under
/aare removed first, then the emptied/a/b, then/aitself./xlies outside the subtree and stays.