Tiny Disk
Implement a class TinyDisk, a small file system. A path is / for the top folder, or names joined by slashes such as /notes/todo. A folder holds files and other folders, and a file holds text. Write these methods:
TinyDisk()starts with an empty top folder.make_folder(path)creates the folder, together with any folders on the way that are missing, and returnsTrue. It also returnsTrueif the folder is already there. If the path, or an earlier part of it, names a file, nothing is created and it returnsFalse.write(path, text)addstextto the end of the file atpathand returnsTrue. A missing file is created first, and so are missing folders on the way. Ifpathnames a folder, or an earlier part of the path names a file, nothing changes and it returnsFalse.read(path)returns the text of the file atpath, or""if there is no file there.list_names(path)returns the names inside the folder atpathin alphabetical order. For a file it returns a list holding just the name of that file, and for a path that does not exist an empty list.remove(path)deletes the file or folder atpath, a folder together with everything inside it, and returns the number of files deleted. It returns0if nothing is there. The top folder cannot be deleted:remove("/")returns0.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
TinyDisk()
write("/notes/todo", "milk") returns true
write("/notes/todo", "eggs") returns true
read("/notes/todo") returns "milkeggs"
make_folder("/notes/todo/x") returns false
make_folder("/pics") returns true
list_names("/") returns ["notes","pics"]
list_names("/notes/todo") returns ["todo"]
remove("/notes") returns 1
list_names("/") returns ["pics"]
The two writes create the folder notes and the file todo, which then reads milkeggs. A folder cannot be made below a file. The top folder lists notes and pics, and listing a file gives its own name. Removing notes deletes the one file inside it.
Constraints
- a path is
/or 1 to 4 names, each written after a slash; a name is 1 to 5 lowercase letters writeis never called with the path/;textis 1 to 4 lowercase letters- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Classes problems
- Tray of WeightsHard
- Two-Tier CacheHard
- Bank Account Without OverdraftsBeginner
- Grid With Row and Column AddsBeginner
- Guest Book SignaturesBeginner
- Odometer With a Trip MeterBeginner
- Panel of Light SwitchesBeginner
- Rectangle on the TableBeginner