Find common directory path: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|J}}: whitespace)
(→‎{{header|J}}: add cross-platform support)
Line 12: Line 12:
=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<lang j>parseDirs =: '/'&= <;.2 ]
<lang j>parseDirs =: (PATHSEP_j_&= <;.2 ])@jhostpath
getCommonPrefix =: ([: *./\ *./@({. ="1 }.)) ;@# {.
getCommonPrefix =: ([: *./\ *./@({. ="1 }.)) ;@# {.



Revision as of 21:35, 23 March 2010

Task
Find common directory path
You are encouraged to solve this task according to the task description, using any language you may know.

Given a set of strings representing directory paths and a single character directory separator; return a string representing that part of the directory tree that is common to all the directories.

Test your routine using the forward slash '/' character as the directory separator and the following three strings as input paths:

 '/home/user1/tmp/coverage/test'
 '/home/user1/tmp/covert/operator'
 '/home/user1/tmp/coven/members'

Note: The resultant path should be a valid directory.
If your language has a routine that performs this function (even if it does not have a changeable separator character, then mention it as part of the task)

J

Solution: <lang j>parseDirs =: (PATHSEP_j_&= <;.2 ])@jhostpath getCommonPrefix =: ([: *./\ *./@({. ="1 }.)) ;@# {.

getCommonDirPath=: [: getCommonPrefix parseDirs&></lang>

Example: <lang j> paths=: '/home/user1/tmp/coverage/test';'/home/user1/tmp/covert/operator';'/home/user1/tmp/coven/members'

  getCommonPrefix >paths

/home/user1/tmp/cove

  getCommonDirPath paths

/home/user1/tmp/</lang>

Python

The Python os.path.commonprefix function is broken as it returns common characters that may not form a valid directory path: <lang python>>>> import os >>> os.path.commonprefix(['/home/user1/tmp/coverage/test', '/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members']) '/home/user1/tmp/cove'</lang>

This result can be fixed: <lang python>>>> def commonprefix(*args, sep='/'): return os.path.commonprefix(*args).rpartition(sep)[0]

>>> commonprefix(['/home/user1/tmp/coverage/test', '/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members']) '/home/user1/tmp'</lang>

But it may be better to not rely on the faulty implementation at all: <lang python>>>> from itertools import takewhile >>> def allnamesequal(name): return all(n==name[0] for n in name[1:])

>>> def commonprefix(paths, sep='/'): bydirectorylevels = zip(*[p.split(sep) for p in paths]) return sep.join(list(zip(*takewhile(allnamesequal, bydirectorylevels)))[0])

>>> commonprefix(['/home/user1/tmp/coverage/test', '/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members']) '/home/user1/tmp'</lang>