3
\$\begingroup\$

Task

In this challenge, your task is to write a program or function which takes an array of paths with an additional boolean indicating it is a file or directory and outputs a file/directory tree in any reasonable format.

Remarks

  • Some directories can end with a forward slash ("/") but this is not always the case
  • A parent directory is not always explicitly defined but should be in the tree
  • "." and ".." as file name or directory name are not allowed
  • Standard loopholes are not allowed

Input

An unsorted array of paths. Each item is a tuple of path and a boolean which is True for a file and False for a directory.

[
    ['Root Folder/Sub Folder/', False],
    ['Root Folder/Sub Folder/hello.txt', True],
    ['Root Folder/Sub Folder/SubSub Folder/SubSubSub Folder/', False],
    ['Root Folder/Sub Folder/world.txt', True],
    ['Root Folder/Sub Folder 2', False],
]

Output

Text formatted example

[D] Root Folder
    [D] Sub Folder
        [D] SubSub Folder
            [D] SubSubSub Folder
        [F] hello.txt
        [F] world.txt
    [D] Sub Folder 2

Data structure formatted example

[
    'Root Folder',
    [
        [
            'Sub Folder',
            [
                [
                    'SubSub Folder',
                    [
                        [
                            'SubSubSub Folder', [], []
                        ]
                    ],
                    []
                ]
            ],
            ['hello.txt', 'world.txt']
        ],
        [
            'Sub Folder 2',
            [],
            []
        ]
    ],
    []
]

This is a code golf. Shortest code in bytes wins.

\$\endgroup\$
9
  • \$\begingroup\$ Related. \$\endgroup\$
    – pxeger
    Jul 5 at 9:19
  • 1
    \$\begingroup\$ The distinction between files and directories, as well as the backslash rule, seem needlessly complex \$\endgroup\$
    – mousetail
    Jul 5 at 9:43
  • 1
    \$\begingroup\$ May I assume the path don't contain /../ or /./ in it? \$\endgroup\$
    – tsh
    Jul 5 at 10:01
  • 1
    \$\begingroup\$ What is expected output? Should I output exactly match the given ascii art? Or which part is necessary? \$\endgroup\$
    – tsh
    Jul 5 at 10:02
  • 1
    \$\begingroup\$ I suggest using our Sandbox in the future, so the issues like in the above comments are resolved there. \$\endgroup\$
    – pajonk
    Jul 5 at 10:08

0

Browse other questions tagged or ask your own question.