FunctionRacerProblemsRace

Folder Total Size

EasyRecursionPython, JavaScript, TypeScript

folder is a dictionary that describes a folder on disk. Each key is a name; its value is either an integer, the size of a file with that name, or another dictionary of the same shape, a sub-folder, nested to any depth. Return the total size of all files anywhere inside folder, counting files in sub-folders, their sub-folders, and so on. An empty folder has total size 0.

You could keep a list of folders still to visit, but the recursive version is natural: add up the files directly inside, and ask each sub-folder for its own total.

For example, {"a.txt": 10, "docs": {"b.txt": 5, "old": {"c.txt": 1}}} gives 10 + 5 + 1 = 16.

Example 1

folder {"a.txt":10,"docs":{"b.txt":5,"old":{"c.txt":1}}}
returns 16

a.txt is 10, docs holds b.txt (5) and the folder old, which holds c.txt (1): 10 + 5 + 1 = 16.

Example 2

folder {"a":1,"b":2}
returns 3

Two files at the top level, 1 + 2 = 3.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems