2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -8,7 +8,8 @@ Test your routine using the forward slash '/' character as the directory separat
|
|||
Note: The resultant path should be the valid directory <code>'/home/user1/tmp'</code> and not the longest common string <code>'/home/user1/tmp/cove'</code>.<br>
|
||||
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.
|
||||
|
||||
'''''See Also:'''''
|
||||
|
||||
;Related tasks
|
||||
* [[Longest common prefix]]
|
||||
|
||||
<br>
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ my @dirs := </home/user1/tmp/coverage/test
|
|||
/home/user1/tmp/covert/operator
|
||||
/home/user1/tmp/coven/members>;
|
||||
|
||||
my @comps := @dirs.map: { [ .comb(/ $sep [ <!before $sep> . ]* /) ] };
|
||||
my @comps = @dirs.map: { [ .comb(/ $sep [ <!before $sep> . ]* /) ] };
|
||||
|
||||
say "The longest common path is ",
|
||||
gather for 0..* -> $column {
|
||||
|
|
|
|||
|
|
@ -1,16 +1,3 @@
|
|||
>>> 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(x[0] for x in takewhile(allnamesequal, bydirectorylevels))
|
||||
|
||||
>>> commonprefix(['/home/user1/tmp/coverage/test',
|
||||
'/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members'])
|
||||
>>> paths = ['/home/user1/tmp/coverage/test', '/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members']
|
||||
>>> os.path.dirname(os.path.commonprefix(paths))
|
||||
'/home/user1/tmp'
|
||||
>>> # And also
|
||||
>>> commonprefix(['/home/user1/tmp', '/home/user1/tmp/coverage/test',
|
||||
'/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members'])
|
||||
'/home/user1/tmp'
|
||||
>>>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
>>> 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(x[0] for x in takewhile(allnamesequal, bydirectorylevels))
|
||||
|
||||
>>> commonprefix(['/home/user1/tmp/coverage/test',
|
||||
'/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members'])
|
||||
'/home/user1/tmp'
|
||||
>>> # And also
|
||||
>>> commonprefix(['/home/user1/tmp', '/home/user1/tmp/coverage/test',
|
||||
'/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members'])
|
||||
'/home/user1/tmp'
|
||||
>>>
|
||||
|
|
@ -1,15 +1,16 @@
|
|||
/*REXX program finds common directory path for a list of files. */
|
||||
@.= /*define all file lists to null. */
|
||||
@.1 = '/home/user1/tmp/coverage/test'
|
||||
@.2 = '/home/user1/tmp/covert/operator'
|
||||
@.3 = '/home/user1/tmp/coven/members'
|
||||
L=length(@.1)
|
||||
do j=2 while @.j\=='' /*start with the second string. */
|
||||
_ = compare(@.j,@.1); if _==0 then iterate /*equal.*/
|
||||
L = min(L,_) /*find the minimum equal strings.*/
|
||||
/*REXX program finds the common directory path for a list of files. */
|
||||
@. = /*the default for all file lists (null)*/
|
||||
@.1 = '/home/user1/tmp/coverage/test'
|
||||
@.2 = '/home/user1/tmp/covert/operator'
|
||||
@.3 = '/home/user1/tmp/coven/members'
|
||||
L=length(@.1) /*use the length of the first string. */
|
||||
do j=2 while @.j\=='' /*start search with the second string. */
|
||||
_=compare(@.j, @.1) /*use REXX compare BIF for comparison*/
|
||||
if _==0 then iterate /*Strings are equal? Then they're equal*/
|
||||
L=min(L, _) /*find the minimum length equal strings*/
|
||||
end /*j*/
|
||||
|
||||
common = left(@.1,lastpos('/',@.1,L)) /*find the shortest DIR string.*/
|
||||
if right(common,1)=='/' then common=left(common, max(0,length(common)-1))
|
||||
say 'common directory path: ' common /*[↑] handle trailing / delimiter*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
common=left( @.1, lastpos('/', @.1,L) ) /*determine the shortest DIR string. */
|
||||
if right(common, 1)=='/' then common=left(common, max(0, length(common) - 1) )
|
||||
say 'common directory path: ' common /* [↑] handle trailing / delimiter*/
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue