H Tree Directories

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):

H tree

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.

Calvin's Hobbies

Posted 2015-01-17T10:26:58.183

Reputation: 84 000

Do the subdirectories of tree always have to be left and right instead of up and down? – KSFT – 2015-01-17T18:54:30.643

1@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

Answers

5

Ruby, 127 bytes

f=->n{n<2?['tree']:f[n-1].map{|p|%w{left right up down}[n%2*2,2].map{|d|p+?/+d}}.flatten}
system'mkdir "'+f[gets.to_i]*'" "'+?"

Tested on Windows. Takes input via STDIN.

f recursively generates a list of the required leaves of the tree, and then I just feed them to a single system call of mkdir.

Martin Ender

Posted 2015-01-17T10:26:58.183

Reputation: 184 808

3

Lua, 179

t=0+io.read()a={"left","right","up","down"}function h(d,v)if v~=t then os.execute("mkdir "..d)x=1 if v%2==1 then x=3 end for g=x,x+1 do h(d.."\\"..a[g],v+1)end end end h("tree",0)

CJStuart

Posted 2015-01-17T10:26:58.183

Reputation: 131

2

Python - 194

from os import system as s
a="mkdir "
s(a+"tree")
def f(n):
 if n<j%2:return
 for i in(0,1):m=["up"if n%2 else"left","rdiogwhnt"[n%2::2]][i];s(a+m);s("cd "+m);f(n-1);s("cd..")
j=input()
f(j+j%2)

KSFT

Posted 2015-01-17T10:26:58.183

Reputation: 1 527

its slightly shorter to do from os import*;s=system – DenDenDo – 2015-01-17T21:39:04.893

Two more things: you can also do ["left","up"][n%2] and you can drop the brackets around (0,1) to give for i in 0,1: – Sp3000 – 2015-01-17T23:18:18.153

2

Python 2 + *nix coreutils, 212 189

Generates all the innermost paths and calls

mkdir -p

import os
n=int(raw_input())-1 
for i in range(2**n):os.system("mkdir -p "+os.path.join('tree',*([['right','left'],['up','down']][b%2][int(j)]for b,j in enumerate('{:0{}b}'.format(i,n)))))

Crashes if input < 1

user80551

Posted 2015-01-17T10:26:58.183

Reputation: 2 520

you can combine the first two lines: import os,itertools as t – DenDenDo – 2015-01-17T21:36:02.597

@DenDenDo Removed itertools completely – user80551 – 2015-01-18T06:55:07.777