3
\$\begingroup\$

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

Bash, 175 169 168 135 130 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 *
read n m
for((i=n+m;i--;));do
read d
mkdir -p .$d
done
echo Case \#$x: $[`find|wc -l`-n-1]
done
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ The challenge is not to create the dirs, only print the ones that need to be created. Nice find though! \$\endgroup\$ – Juliana Peña Feb 16 '11 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.