11
Contest (!): In the language of your choice, write a program that will traverse the directory tree of a given directory and output a tree (i.e., an array of arrays) corresponding to it. Assume the directory is a predefined variable D. Smallest character count wins.
Rules:
- You must use recursion
- See Rules
Note: Assume that there are no recursion depth limits. In other words, your code just needs to work for sufficiently small directory trees, and in principle for larger ones.
For instance:
Directory tree is
dir1
├── dir11
│ ├── file111
│ └── file112
├── dir12
│ ├── file121
│ ├── file122
│ └── file123
├── file11
├── file12
└── file13
Output tree is
[[[],[]],[[],[],[]],[],[],[]]
First code golf here so lemme know if I'm doing something wrong.
Have fun :)
Windows Batch/CMD, 4 bytes:
tree
– cat – 2015-12-19T13:36:39.3137"Rules: 1. You must use recursion 2. See rules" Ah!! HELP I'M STUCK IN AN INFINITE LOOP! – Justin – 2014-01-08T07:42:07.100
1You could go by the character count, or you could go by smallest size in bytes (this way programs with unicode characters are larger than if they used pure ascii) – Justin – 2014-01-08T07:43:21.213
1How deep would it traverse? – It'sNotALie. – 2014-01-08T07:47:03.890
Many people would appreciate it if you instead gave an input of a file (as a path, or something else) and they could simply output it. Also, your output seems a bit hard to understand. Can you provide a test case? Rather than using an array of arrays, could we simply print each directory/file on its own line, but indented to show subfolderness? Basically, must we output in a certain format (in which case, give an example), or can we choose a format (as long as it is unambiguous)? – Justin – 2014-01-08T07:48:19.850
@Quincunx: Edited for clarity – Andrew Odesky – 2014-01-08T07:58:47.743
@It'sNotALie: See added note – Andrew Odesky – 2014-01-08T08:01:18.507
3I'm going blind, parsing your output format. This, from someone who enjoys Lisp. – Darren Stone – 2014-01-08T08:41:59.790
can there be multiple level 0 directories?? – Vogel612 – 2014-01-08T11:07:04.420
@Vogel612: No, there is one specified level 0 directory D to be parsed – Andrew Odesky – 2014-01-08T11:17:47.857