12
1
Programmers are often obsessed with drawing fractals. I think we need a new computer based medium.
The H tree is a fairly simple type of fractal made of horizontal and vertical lines. Here it is at it's tenth iteration (courtesy Wikipedia):
Now, imagine each of the lines in the image is a directory (folder) in a standard computer file system. All but the smallest lines intersect two lines smaller than themselves; these two smaller lines are subdirectories of the larger line. Thus the large horizontal line in the middle is the parent directory of the two largest vertical lines, which are in turn parents, grandparents, etc. of rest of the lines in the image.
Challenge
Write a program that takes in a positive integer N via stdin or the command line (or closest alternative) and creates a directory tree that mirrors the Nth iteration of the H tree fractal.
The first iteration (N = 1) is a single horizontal line. In each subsequent iteration a new set of vertical or horizontal lines is added to the ends of the current lines. So for N = 2 two vertical lines are added (making an H shape), for N = 3 four horizontal lines are added to that, etc.
The name of the root directory must always be tree
. The names of the subdirectories must match the direction they are in with respect to their parent line, using right
, left
, up
and down
.
Since the root directory is always a horizontal line it will always have right
and left
subdirectories. But those two will have up
and down
subdirectories, and those will have right
and left
again, and so on.
The directories at the end of the limit of iterations should be empty.
Example
For N = 3 the directory tree should look like this:
tree
right
up
down
left
up
down
Additional Info
- Ideally the
tree
directory will pop up in the same folder the source code is in, but it's alright if it goes to some working directory. - You may assume that there is no pre-existing
tree
directory in the place yours will be created. - You may assume that your program will always be run on the same common modern operating system (Mac/Windows/Linux). Basically don't worry about using a file separator that doesn't work in all OS's.
Scoring
This is code-golf, so the shortest code in bytes wins.
Do the subdirectories of
tree
always have to beleft
andright
instead ofup
anddown
? – KSFT – 2015-01-17T18:54:30.6431@KSFT Yes: "Since the root directory is always a horizontal line it will always have right and left subdirectories." – Calvin's Hobbies – 2015-01-17T19:20:55.370