Say that you have this folder structure on your coumputer:


And you want to have these folders:


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.


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


1 3


Directories you need to create


  • 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

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
rm -r *
read n m
read d
mkdir -p .$d
echo Case \#$x: $[`find|wc -l`-n-1]
| improve this answer | |


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 = 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
| improve this answer | |
  • \$\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.