# Directories required for creating a folder tree

Say that you have this folder structure on your coumputer:

/a


And you want to have these folders:

/a/b
/a/c
/b/b


You would need to create four directories, using mkdir for example:

mkdir /a/b
mkdir /a/c
mkdir /b
mkdir /b/b


Write a program that, given existing and required directory structures, will output the directories that need to be created.

## Input

First line has two numbers: n, number of existing directories, and m, number of required directories Next n lines has the list of existing directories Next m lines has the list of required directories

Example:

1 3
/a
/a/b
/a/c
/b/b


## Output

Directories you need to create

Example:

/a/b
/a/c
/b
/b/b

• Exact duplicate of "File Fix-it codegolf (GCJ 2010 1B-A)" from StackOverflow, which in turn came from Google Code Jam Round 1B 2010. – Joey Adams Feb 16 '11 at 4:10
• @Joey, this one is not a code-golf, so we can write more efficient (cpu cycles) answers than in the SO question – gnibbler Feb 16 '11 at 9:44
• errr... who wins, then? O:-) – J B Feb 16 '11 at 9:45
• @J B, probably not one that formats your harddrive :) – gnibbler Feb 16 '11 at 9:47
• @gnibbler but how can you trust algorithmics when it's so easy to just try it out? O:-) – J B Feb 16 '11 at 16:24

# Bash, 175169168135130 128

Here's one of my worthwhile solutions from SO, then:

WARNING: Be sure to run in an empty directory, as this will wipe out its contents first thing per test.

read t
for((;x++<t;));do
rm -r *
for((i=n+m;i--;));do
mkdir -p .$d done echo Case \#$x: \$[find|wc -l-n-1]
done


# Python

In Python, you would just use os.makedirs(). See the docs at the bottom of this post

import sys

num_existing, num_required = map(int, next(sys.stdin).split())

sep = "/"
directory = {}

for i,path in enumerate(s.rstrip("\n") for s in sys.stdin):
d = directory
base = ""
for p in path.split(sep)[1:]:
base += sep+p
if p not in d:
if i >= num_existing:
print base
d[p]={}
d = d[p]


Python docs for os.makedirs()

>>> import os
>>> help(os.makedirs)

Help on function makedirs in module os:

makedirs(name, mode=511)
makedirs(path [, mode=0777])

Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir, except that any intermediate path segment (not
just the rightmost) will be created if it does not exist.  This is
recursive.

• The challenge is not to create the dirs, only print the ones that need to be created. Nice find though! – Juliana Peña Feb 16 '11 at 3:30