Jump to content

Find common directory path: Difference between revisions

m
syntax highlighting fixup automation
(Find common directory path in Yabasic)
m (syntax highlighting fixup automation)
Line 17:
{{trans|C}}
 
<langsyntaxhighlight lang="11l">F find_common_directory_path(paths, sep = ‘/’)
V pos = 0
L
Line 34:
‘/home/user1/tmp/coverage/test’,
‘/home/user1/tmp/covert/operator’,
‘/home/user1/tmp/coven/members’]))</langsyntaxhighlight>
 
{{out}}
Line 42:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">
with Ada.Text_IO; use Ada.Text_IO;
 
Line 80:
);
end Test_Common_Path;
</syntaxhighlight>
</lang>
Output:
<pre>
Line 87:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">cdp(...)
{
integer e;
Line 111:
 
0;
}</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 120:
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
 
<langsyntaxhighlight lang="algol68"># Utilities code #
 
CHAR dir sep = "/"; # Assume POSIX #
Line 177:
);
print((dir name(common prefix(dir list)), new line))
)</langsyntaxhighlight>
Output:
<pre>
Line 185:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">commonPathPrefix: function [lst][
paths: map lst => [split.by:"/" &]
 
Line 207:
"/home/user1/tmp/covert/operator"
"/home/user1/tmp/coven/members"
]</langsyntaxhighlight>
 
{{out}}
Line 214:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">Dir1 := "/home/user1/tmp/coverage/test"
Dir2 := "/home/user1/tmp/covert/operator"
Dir3 := "/home/user1/tmp/coven/members"
Line 228:
Else Break
 
MsgBox, % Result</langsyntaxhighlight>
Message box shows:
<pre>/home/user1/tmp</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk"># Finds the longest common directory of paths[1], paths[2], ...,
# paths[count], where sep is a single-character directory separator.
function common_dir(paths, count, sep, b, c, f, i, j, p) {
Line 273:
a[3] = "/home/user1/tmp/coven/members"
print common_dir(a, 3, "/")
}</langsyntaxhighlight>
 
Prints <tt>/home/user1/tmp</tt>.
Line 285:
Also, under FreeBASIC, the <code>pathSep</code> arg to <code>commonPath$</code> could be made optional, or even system-dependent.
 
<langsyntaxhighlight lang="qbasic">DECLARE FUNCTION commonPath$ (paths() AS STRING, pathSep AS STRING)
 
DATA "/home/user2", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members"
Line 336:
commonPath$ = tmpstr1
END IF
END FUNCTION</langsyntaxhighlight>
 
=={{header|BASIC256}}==
{{trans|GW-BASIC}}
<langsyntaxhighlight BASIC256lang="basic256">x = "/home/user1/tmp/coverage/test"
y = "/home/user1/tmp/covert/operator"
z = "/home/user1/tmp/coven/members"
Line 365:
REM Task description says no trailing slash, so...
a = i - 1
print "Common path is '"; left(x, a); "'"</langsyntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">
@echo off
setlocal enabledelayedexpansion
Line 430:
endlocal
exit /b
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 437:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> DIM path$(3)
path$(1) = "/home/user1/tmp/coverage/test"
Line 455:
NEXT J%
UNTIL I% = 0
= LEFT$(p$(1), O%-1)</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
int common_len(const char *const *names, int n, char sep)
Line 491:
 
return 0;
}</langsyntaxhighlight>output:<syntaxhighlight lang="text">Common path: /home/user1/tmp</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
 
<langsyntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
Line 559:
}
 
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <string>
Line 592:
std::string::size_type found = compareString.rfind( separator , maxCharactersCommon ) ;
return compareString.substr( 0 , found ) ;
}</langsyntaxhighlight>
Output:
<pre>
Line 599:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(use '[clojure.string :only [join,split]])
 
(defn common-prefix [sep paths]
Line 612:
["/home/user1/tmp/coverage/test"
"/home/user1/tmp/covert/operator"
"/home/user1/tmp/coven/members"]))</langsyntaxhighlight>
 
 
=={{header|Common Lisp}}==
The strings represent file paths, so instead of treating them as simple strings, this uses the specialized pathname functions, which are more robust and generic.
<langsyntaxhighlight lang="lisp">
(defun common-directory-path (&rest paths)
(do* ((pathnames (mapcar #'(lambda (path) (cdr (pathname-directory (pathname path)))) paths)) ; convert strings to lists of subdirectories
Line 624:
((null (cdr rem)) (make-pathname :directory (cons :absolute (subseq (first pathnames) 0 pos)))) ; take the common sublists and convert back to a pathname
(setq pos (min pos (mismatch (first rem) (second rem) :test #'string-equal))) )) ; compare two paths
</syntaxhighlight>
</lang>
 
 
Line 633:
=={{header|D}}==
This code uses the std.algorithm.commonPrefix function that finds the common prefix of two ranges.
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.algorithm, std.path, std.array;
 
string commonDirPath(in string[] paths, in string sep = "/") pure {
Line 646:
"/home/user1/tmp/coven/members"];
writeln(`The common path is: "`, paths.commonDirPath, '"');
}</langsyntaxhighlight>
{{out}}
<pre>The common path is: "/home/user1/tmp"</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Find_common_directory_path;
 
Line 711:
'/home/user1/tmp/coven/members']));
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>/home/user1/tmp</pre>
Line 717:
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule RC do
def common_directory_path(dirs, separator \\ "/") do
dir1 = Enum.min(dirs) |> String.split(separator)
Line 727:
 
dirs = ~w( /home/user1/tmp/coverage/test /home/user1/tmp/covert/operator /home/user1/tmp/coven/members )
IO.inspect RC.common_directory_path(dirs)</langsyntaxhighlight>
 
{{out}}
Line 735:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( find_common_directory ).
 
Line 747:
 
keep_common( Components, Acc ) -> [X || X <- Components, Y <- Acc, X =:= Y].
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 755:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
let (|SeqNode|SeqEmpty|) s =
Line 784:
 
printfn "The common preffix is: %A" (String.Join("/", (commonPrefix args)))
0</langsyntaxhighlight>
Output for the given task input
<pre>The common preffix is: "/home/user1/tmp"</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">: take-shorter ( seq1 seq2 -- shorter )
[ shorter? ] 2keep ? ;
 
Line 799:
 
: common-prefix ( seq separator -- prefix )
[ ] swap '[ _ common-prefix-1 ] map-reduce ;</langsyntaxhighlight>
 
( scratchpad ) {
Line 810:
=={{header|FreeBASIC}}==
{{Trans|Visual Basic}}
<langsyntaxhighlight lang="freebasic">
' compile: fbc.exe -s console cdp.bas
 
Line 875:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>/home/user1/tmp/coverage/test
Line 894:
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sFolder As String[] = ["/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members"]
Dim sSame As String
Line 910:
Print Mid(sSame, 1, RInStr(sSame, "/") - 1)
 
End</langsyntaxhighlight>
Output:
<pre>
Line 921:
E.g. (<code>/home/user1, /home/user1/foo, /home/user1/bar</code>) should result in <code>/home/user1</code>, not <code>/home</code>.
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 999:
fmt.Println("Common path:", c)
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def commonPath = { delim, Object[] paths ->
def pathParts = paths.collect { it.split(delim) }
pathParts.transpose().inject([match:true, commonParts:[]]) { aggregator, part ->
Line 1,010:
aggregator
}.commonParts.join(delim)
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">println commonPath('/',
'/home/user1/tmp/coverage/test',
'/home/user1/tmp/covert/operator',
Line 1,022:
'/home/user1/tmp/covert/test',
'/home/user1/tmp/coven/test',
'/home/user1/tmp/covers/test')</langsyntaxhighlight>
 
Output:
Line 1,037:
Note that if the root directory is the common path, this reports the same as no match found (i.e. blank result).
 
<langsyntaxhighlight lang="qbasic">10 REM All GOTO statements can be replaced with EXIT FOR in newer BASICs.
 
110 X$ = "/home/user1/tmp/coverage/test"
Line 1,065:
350 A = L0 - 1
360 P$ = LEFT$(X$, A)
370 PRINT "Common path is '"; P$; "'"</langsyntaxhighlight>
 
Output:
Line 1,072:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List
 
-- Return the common prefix of two lists.
Line 1,094:
"/home/user1/tmp/covert/operator",
"/home/user1/tmp/coven/members"
]</langsyntaxhighlight>
 
Or, expressed directly in applicative terms:
<langsyntaxhighlight lang="haskell">import Data.List (transpose, intercalate)
import Data.List.Split (splitOn)
 
Line 1,117:
, "/home/user1/tmp/covert/operator"
, "/home/user1/tmp/coven/members"
]</langsyntaxhighlight>
{{Out}}
<pre>/home/user1/tmp</pre>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">CHARACTER a='/home/user1/tmp/coverage/test', b='/home/user1/tmp/covert/operator', c='/home/user1/tmp/coven/members'
 
minLength = MIN( LEN(a), LEN(b), LEN(c) )
Line 1,135:
WRITE(Messagebox, Name) "No common directory for", a, b, c
ENDIF
ENDDO</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
write(lcdsubstr(["/home/user1/tmp/coverage/test","/home/user1/tmp/covert/operator","/home/user1/tmp/coven/members"]))
end
Line 1,157:
if not match(s,x) then fail
return s
end</langsyntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">parseDirs =: = <;.2 ]
getCommonPrefix =: {. ;@{.~ 0 i.~ *./@(="1 {.)
 
getCommonDirPath=: [: getCommonPrefix parseDirs&></langsyntaxhighlight>
 
'''Example:'''
<langsyntaxhighlight 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/</langsyntaxhighlight>
 
'''Note:'''
This alternative formulation of parseDirs provides cross-platform support, without the need to specify the path separator.
<langsyntaxhighlight lang="j">parseDirs =: (PATHSEP_j_&= <;.2 ])@jhostpath</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
This example is case-sensitive.
<langsyntaxhighlight lang="java5">public class CommonPath {
public static String commonPath(String... paths){
String commonPath = "";
Line 1,218:
System.out.println(commonPath(paths2));
}
}</langsyntaxhighlight>
Output:
<pre>/home/user1/tmp/
Line 1,225:
 
A slightly modified version of the previous program, only the method commonPath() is changed.
<langsyntaxhighlight lang="java5">
static String commonPath(String... paths){
String commonPath = "";
Line 1,244:
return commonPath;
}
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">
/**
* Given an array of strings, return an array of arrays, containing the
Line 1,293:
 
console.log(`Common path is: ${commonPath(cdpInput)}`);
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,301:
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq"># maximal_initial_subarray takes as input an array of arrays:
def maximal_initial_subarray:
(map( .[0] ) | unique) as $u
Line 1,314:
[.[] | split(slash)] | maximal_initial_subarray | join(slash) ;
 
common_path("/")</langsyntaxhighlight>
 
Assuming the above jq program is in a file named common_path.jq and that the file directories.txt contains the three given directory strings quoted with double quotation marks:
<langsyntaxhighlight lang="jq">$ jq -s -f common_path.jq directories.txt
"home/user1/tmp"</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function commonpath(ds::Vector{<:AbstractString}, dlm::Char='/')
0 < length(ds) || return ""
1 < length(ds) || return String(ds[1])
Line 1,342:
 
println("Comparing:\n - ", join(test, "\n - "))
println("for their common directory path yields:\n", commonpath(test))</langsyntaxhighlight>
 
{{out}}
Line 1,353:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
fun findCommonDirPath(paths: List<String>, separator: Char): String {
Line 1,380:
println("The common directory path of:\n\n$pathsToPrint\n")
println("is '${findCommonDirPath(paths, '/')}'")
}</langsyntaxhighlight>
 
{{out}}
Line 1,394:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">#!/usr/bin/lasso9
 
local(
Line 1,410:
}
 
stdoutnl(commonpath(#path1, #path2, #path3))</langsyntaxhighlight>
 
Output:
Line 1,416:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">path$(1) = "/home/user1/tmp/coverage/test"
path$(2) = "/home/user1/tmp/covert/operator"
path$(3) = "/home/user1/tmp/coven/members"
Line 1,440:
end if
wend
end function</langsyntaxhighlight>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">on getCommonPath (pathes, sep)
_player.itemDelimiter = sep
 
Line 1,461:
end repeat
return pathes[1].item[1..commonCnt]
end</langsyntaxhighlight>
<langsyntaxhighlight lang="lingo">pathes = []
pathes.add("/home/user1/tmp/coverage/test")
pathes.add("/home/user1/tmp/covert/operator")
Line 1,468:
 
put getCommonPath(pathes, "/")
-- "/home/user1/tmp"</langsyntaxhighlight>
 
=={{header|MapBasic}}==
Line 1,474:
Derived from the [https://www.rosettacode.org/wiki/Find_common_directory_path#BASIC BASIC] example above
 
<langsyntaxhighlight lang="qbasic">Include "MapBasic.def"
 
Declare Sub Main
Line 1,539:
PRINT "Common path is " + commonPath(x(), Sep)
 
End Sub</langsyntaxhighlight>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
<lang Maple>
dirpath:=proc(a,b,c)
local dirtemp,dirnew,x;
Line 1,552:
end use;
end proc;
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">FindCommonDirectory[x_] := If[StringTake[#, -1] != "/", StringTake[#, Max[StringPosition[#, "/"]]], #] &
[Fold[LongestCommonSubsequence, First[x] , Rest[x]]]
 
FindCommonDirectory[{"/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members"}]
->"/home/user1/tmp/"</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
<lang Matlab>
function lcp = longest_common_dirpath(varargin)
ix = find(varargin{1}=='/');
Line 1,574:
 
longest_common_dirpath('/home/user1/tmp/coverage/test', '/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members')
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,582:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">scommon(a, b) := block([n: min(slength(a), slength(b))],
substring(a, 1, catch(for i thru n do (
if not cequal(charat(a, i), charat(b, i)) then throw(i)), n + 1)))$
Line 1,592:
 
commonpath(["c:/files/banister.jpg", "c:/files/bank.xls", "c:/files/banana-recipes.txt"]);
"c:/files"</langsyntaxhighlight>
 
=={{header|MUMPS}}==
<syntaxhighlight lang="mumps">FCD
<lang MUMPS>FCD
NEW D,SEP,EQ,LONG,DONE,I,J,K,RETURN
SET D(1)="/home/user1/tmp/coverage/test"
Line 1,609:
WRITE !,"The longest common directory is: ",RETURN
KILL D,SEP,EQ,LONG,DONE,I,J,K,RETURN
QUIT</langsyntaxhighlight>
Usage:<pre>
USER>D FCD^ROSETTA
Line 1,620:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strutils
 
proc commonprefix(paths: openarray[string], sep = "/"): string =
Line 1,632:
result = result[0 .. result.rfind(sep)]
 
echo commonprefix(@["/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members"])</langsyntaxhighlight>
Output:
<pre>/home/user1/tmp</pre>
Line 1,638:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let rec aux acc paths =
if List.mem [] paths
then (List.rev acc) else
Line 1,664:
] in
print_endline (common_prefix "/" dirs);
;;</langsyntaxhighlight>
 
(uses the module <code>[http://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html Str]</code>, str.cma)
 
=={{header|OpenEdge/Progress}}==
<langsyntaxhighlight lang="progress">FUNCTION findCommonDir RETURNS CHAR(
i_cdirs AS CHAR,
i_cseparator AS CHAR
Line 1,699:
RETURN cresult.
 
END FUNCTION.</langsyntaxhighlight>
 
<langsyntaxhighlight lang="progress">MESSAGE
findCommonDir(
'/home/user1/tmp/coverage/test' + '~n' +
Line 1,708:
'/'
)
VIEW-AS ALERT-BOX</langsyntaxhighlight>
 
Output
Line 1,722:
=={{header|Oz}}==
With a few helper functions, we can express the solution like this in Oz:
<langsyntaxhighlight lang="oz">declare
fun {CommonPrefix Sep Paths}
fun {GetParts P} {String.tokens P Sep} end
Line 1,753:
["/home/user1/tmp/coverage/test"
"/home/user1/tmp/covert/operator"
"/home/user1/tmp/coven/members"]}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">cdp(v)={
my(s="");
v=apply(t->Vec(t),v);
Line 1,767:
if(vecmax(apply(length,v))==vecmin(apply(length,v)),concat(v[1]),s)
};
cdp(["/home/user1/tmp/coverage/test","/home/user1/tmp/covert/operator","/home/user1/tmp/coven/members"])</langsyntaxhighlight>
 
=={{header|Perl}}==
Line 1,773:
A solution which lets the regex engine do all the work ''(it operates on the concatenation of the given paths delimited by null-bytes, which should be safe since null-bytes are not allowed inside paths)'':
 
<langsyntaxhighlight Perllang="perl">sub common_prefix {
my $sep = shift;
my $paths = join "\0", map { $_.$sep } @_;
$paths =~ /^ ( [^\0]* ) $sep [^\0]* (?: \0 \1 $sep [^\0]* )* $/x;
return $1;
}</langsyntaxhighlight>
 
A more conventional solution, which tallies up all potential prefixes from the given paths and then looks for the longest one that occurred the same number of times as there are paths:
 
<langsyntaxhighlight Perllang="perl">use List::Util qw(first);
 
sub common_prefix {
Line 1,793:
return first { $prefixes{$_} == @paths } reverse sort keys %prefixes;
}</langsyntaxhighlight>
 
'''Testing:'''
 
<langsyntaxhighlight lang="perl">my @paths = qw(/home/user1/tmp/coverage/test
/home/user1/tmp/covert/operator
/home/user1/tmp/coven/members);
print common_prefix('/', @paths), "\n";</langsyntaxhighlight>
 
{{out}}
Line 1,809:
To change that behaviour, simply remove both the [1..-2]<br>
For cross-platform operation, simply use the split_path and join_path builtins (not pwa/p2js compatible) instead of split and join.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">common_directory_path</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">paths</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">sep</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'/'</span><span style="color: #0000FF;">)</span>
Line 1,833:
<span style="color: #008000;">"/home/user1/tmp/coven/members"</span><span style="color: #0000FF;">}</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">common_directory_path</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,840:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
 
/*
Line 1,896:
}
 
?></langsyntaxhighlight>
 
 
<langsyntaxhighlight lang="php"><?php
 
/* A more compact string-only version, which I assume would be much faster */
Line 1,920:
}
 
?></langsyntaxhighlight>
 
=={{header|Picat}}==
Line 1,927:
===Using maxof/2===
Using <code>maxof/2</code> to get the longest common prefix.
<langsyntaxhighlight Picatlang="picat">find_common_directory_path(Dirs) = Path =>
maxof( (common_prefix(Dirs, Path,Len), append(_,"/",Path)), Len).
 
Line 1,938:
append(Prefix,_,L)
end,
Len = Prefix.length.</langsyntaxhighlight>
 
===Mode-directed tabling===
Using mode-directed tabling for maximizing the length.
<langsyntaxhighlight Picatlang="picat">find_common_directory_path2(Dirs) = Path =>
common_prefix2(Dirs,Path,'/',_Len).
 
Line 1,957:
Prefix.last() == Last
end,
Len = Prefix.length.</langsyntaxhighlight>
 
For the directories
Line 1,972:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de commonPath (Lst Chr)
(glue Chr
(make
(apply find
(mapcar '((L) (split (chop L) Chr)) Lst)
'(@ (or (pass <>) (nil (link (next))))) ) ) ) )</langsyntaxhighlight>
Output:
<pre>(commonPath
Line 1,989:
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">array paths = ({ "/home/user1/tmp/coverage/test",
"/home/user1/tmp/covert/operator",
"/home/user1/tmp/coven/members" });
Line 1,999:
string cp = String.common_prefix(paths);
cp = cp[..sizeof(cp)-search(reverse(cp), "/")-2];
Result: "/home/user1/tmp"</langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
{{Trans|Visual Basic}}
<langsyntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
Line 2,070:
CON.WAITKEY$
 
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre>/home/user1/tmp/coverage/test
Line 2,090:
=={{header|PowerShell}}==
 
<syntaxhighlight lang="powershell">
<lang Powershell>
<#
.Synopsis
Line 2,118:
}
}
</syntaxhighlight>
</lang>
 
Sample execution:
<syntaxhighlight lang="text">
"C:\a\b\c\d\e","C:\a\b\e\f","C:\a\b\c\d\x" | Get-CommonPath
C:\a\b
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
 
<langsyntaxhighlight lang="prolog">
 
%! directory_prefix(PATHs,STOP0,PREFIX)
Line 2,179:
.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,192:
 
Simply by checking the catalog names until they mismatch and add up the correct parts, the task is accomplished.
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.s CommonPath(Array InPaths.s(1),separator.s="/")
Protected SOut$=""
Protected i, j, toggle
Line 2,214:
Next
ForEver
EndProcedure</langsyntaxhighlight>
 
Example of implementation
<langsyntaxhighlight PureBasiclang="purebasic">Dim t.s(2)
t(0)="/home/user1/tmp/coverage/test"
t(1)="/home/user1/tmp/covert/operator"
t(2)="/home/user1/tmp/coven/members"
 
Debug CommonPath(t(),"/"))</langsyntaxhighlight>
 
=={{header|Python}}==
 
Since Python 3.5 os.path.commonpath function can be used:
<langsyntaxhighlight lang="python">>>> import os
>>> os.path.commonpath(['/home/user1/tmp/coverage/test',
'/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members'])
'/home/user1/tmp'</langsyntaxhighlight>
 
The Python os.path.commonprefix function is [http://nedbatchelder.com/blog/201003/whats_the_point_of_ospathcommonprefix.html broken] as it returns common characters that may not form a valid directory path:
<langsyntaxhighlight 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'</langsyntaxhighlight>
 
This result can be fixed:
<langsyntaxhighlight 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'</langsyntaxhighlight>
 
Even shorter:
<langsyntaxhighlight lang="python">>>> 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'</langsyntaxhighlight>
 
But it may be better to not rely on the faulty implementation at all:
<langsyntaxhighlight lang="python">>>> from itertools import takewhile
>>> def allnamesequal(name):
return all(n==name[0] for n in name[1:])
Line 2,267:
'/home/user1/tmp/covert/operator', '/home/user1/tmp/coven/members'])
'/home/user1/tmp'
>>> </langsyntaxhighlight>
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang r>
get_common_dir <- function(paths, delim = "/")
{
Line 2,293:
get_common_dir(paths) # "/home/user1/tmp"
 
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 2,315:
"/home/user1/tmp/coven/members")
;; --> "/home/user1/tmp"
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my $sep = '/';
my @dirs = </home/user1/tmp/coverage/test
/home/user1/tmp/covert/operator
Line 2,334:
 
say "The longest common path is $prefix";
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,340:
</pre>
If you'd prefer a pure FP solution without side effects, you can use this:
<syntaxhighlight lang="raku" perl6line>my $sep := '/';
my @dirs := </home/user1/tmp/coverage/test
/home/user1/tmp/covert/operator
Line 2,351:
last unless all(@comps[*]»[$column]) eq @comps[0][$column];
take @comps[0][$column] // last;
}</langsyntaxhighlight>
Or here's another factoring, that focuses on building the result with cumulative sequences and getting the solution with `first`:
<syntaxhighlight lang="raku" perl6line>my $sep = '/';
my @dirs = </home/user1/tmp/coverage/test
/home/user1/tmp/covert/operator
Line 2,360:
sub is_common_prefix { so $^prefix eq all(@dirs).substr(0, $prefix.chars) }
 
say ([\~] @dirs.comb(/ $sep [ <!before $sep> . ]* /)).reverse.first: &is_common_prefix</langsyntaxhighlight>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*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'
Line 2,381:
if common=='' then common= "/" /*if no common directory, assume home. */
say 'common directory path: ' common /* [↑] handle trailing / delimiter*/
/*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,388:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Find common directory path
 
Line 2,413:
end
return left(p[1], o-1)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,422:
Uses the standard library <code>[http://www.ruby-doc.org/stdlib/libdoc/abbrev/rdoc/index.html abbrev]</code> module: Given a set of strings, calculate the set of unambiguous abbreviations for those strings, and return a hash where the keys are all the possible abbreviations and the values are the full strings.
 
<langsyntaxhighlight lang="ruby">require 'abbrev'
 
dirs = %w( /home/user1/tmp/coverage/test /home/user1/tmp/covert/operator /home/user1/tmp/coven/members )
 
common_prefix = dirs.abbrev.keys.min_by {|key| key.length}.chop # => "/home/user1/tmp/cove"
common_directory = common_prefix.sub(%r{/[^/]*$}, '') # => "/home/user1/tmp"</langsyntaxhighlight>
 
Implementing without that module:
<langsyntaxhighlight lang="ruby">separator = '/'
path0, *paths = dirs.collect {|dir| dir.split(separator)}
uncommon_idx = path0.zip(*paths).index {|dirnames| dirnames.uniq.length > 1}
uncommon_idx = path0.length unless uncommon_idx # if uncommon_idx==nil
common_directory = path0[0...uncommon_idx].join(separator) # => "/home/user1/tmp"</langsyntaxhighlight>
 
or method version
<langsyntaxhighlight lang="ruby">def common_directory_path(dirs, separator='/')
dir1, dir2 = dirs.minmax.map{|dir| dir.split(separator)}
dir1.zip(dir2).take_while{|dn1,dn2| dn1==dn2}.map(&:first).join(separator)
end
 
p common_directory_path(dirs) #=> "/home/user1/tmp"</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">' ------------------------------------------
' Find common directory to all directories
' and directories common with other Paths
Line 2,530:
html "</TABLE>"
wait
end</langsyntaxhighlight>
========= Common paths ================<br />
shows only the first few common paths..
Line 2,571:
Rust has specific types for owned and borrowed paths. PathBuf is an 'owned' pointer to a path, Path is a borrow; this is similar to String and str, respectively.
 
<syntaxhighlight lang="rust">
<lang Rust>
use std::path::{Path, PathBuf};
 
Line 2,622:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
===Naive===
This simple solution solves the task as given, but has oddities for edge cases due to the implementation of java.lang.String#split.
<langsyntaxhighlight Scalalang="scala">object FindCommonDirectoryPath extends App {
def commonPath(paths: List[String]): String = {
def common(a: List[String], b: List[String]): List[String] = (a, b) match {
Line 2,643:
)
println(commonPath(test))
}</langsyntaxhighlight>
Output:
<pre>/home/user1/tmp</pre>
===Advanced===
This implementation will handle various edge cases and relative paths. It also includes any common trailing '/' but callers can remove this if desired.
<langsyntaxhighlight Scalalang="scala">object FindCommonDirectoryPathRelative extends App {
def commonPath(paths: List[String]): String = {
val SEP = "/"
Line 2,697:
assert(commonPath(List("/a/a/", "/a/b/")) == "/a/")
assert(commonPath(List("/a/b/", "/a/b/")) == "/a/b/")
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 2,707:
but they need to make sure that a path does not end with a slash.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func integer: commonLen (in array string: names, in char: sep) is func
Line 2,748:
writeln("Common path: " <& names[1][.. length]);
end if;
end func;</langsyntaxhighlight>
 
Output:
Line 2,756:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var dirs = %w(
/home/user1/tmp/coverage/test
/home/user1/tmp/covert/operator
Line 2,764:
var unique_pref = dirs.map{.split('/')}.abbrev.min_by{.len};
var common_dir = [unique_pref, unique_pref.pop][0].join('/');
say common_dir; # => /home/user1/tmp</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">fun takeWhileEq ([], _) = []
| takeWhileEq (_, []) = []
| takeWhileEq (x :: xs, y :: ys) =
Line 2,787:
]
 
val () = print (commonPath #"/" paths ^ "\n")</langsyntaxhighlight>
 
=={{header|Swift}}==
Line 2,793:
The below solution works only in swift in Linux.
 
<langsyntaxhighlight lang="swift">import Foundation
 
 
Line 2,809:
 
var output:String = getPrefix(test)!
print(output)</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
proc pop {varname} {
upvar 1 $varname var
Line 2,830:
}
return [join $parts $separator]
}</langsyntaxhighlight>
 
<pre>% common_prefix {/home/user1/tmp/coverage/test /home/user1/tmp/covert/operator /home/user1/tmp/coven/members}
Line 2,836:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
common=""
Line 2,851:
ENDIF
ENDLOOP
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,859:
=={{header|UNIX Shell}}==
The following is a pure Bourne Shell solution. The while loop controls the maximum depth to check paths.
<langsyntaxhighlight lang="bash">
#!/bin/sh
 
Line 2,880:
i=`expr $i + 1`
done
</syntaxhighlight>
</lang>
 
=={{header|Ursala}}==
The algorithm is to lex the paths into component directory names, and then find the greatest common prefix of those.
<langsyntaxhighlight Ursalalang="ursala">#import std
 
comdir"s" "p" = mat"s" reduce(gcp,0) (map sep "s") "p"</langsyntaxhighlight>
where <code>"s"</code> is a dummy variable representing the separator, <code>"p"</code> is a dummy variable representing the list of paths, and
*<code>sep</code> is second order function in the standard library that takes a separator character and returns a lexer mapping a string containing the separator to a list of the substrings found between occurrences of it
Line 2,894:
*<code>mat</code> is a second order function in the standard library that takes a separator character and returns a function that flattens a list of strings into a single string with copies of the separator inserted between them
Here is a version using operators instead of mnemonics for <code>map</code> and <code>reduce</code>.
<langsyntaxhighlight Ursalalang="ursala">comdir"s" "p" = mat"s" gcp:-0 sep"s"* "p"</langsyntaxhighlight>
Here is one in partly point-free form, using the composition operator (<code>+</code>).
<langsyntaxhighlight Ursalalang="ursala">comdir"s" = mat"s"+ gcp:-0+ sep"s"*</langsyntaxhighlight>
Here it is in point-free form.
<langsyntaxhighlight Ursalalang="ursala">comdir = +^/mat gcp:-0++ *+ sep</langsyntaxhighlight>
test program:
<langsyntaxhighlight Ursalalang="ursala">#cast %s
 
test =
Line 2,907:
'/home/user1/tmp/coverage/test',
'/home/user1/tmp/covert/operator',
'/home/user1/tmp/coven/members'></langsyntaxhighlight>
output:
<pre>'/home/user1/tmp'</pre>
Line 2,915:
=={{header|VBScript}}==
{{works with|Windows Script Host|*}}
<syntaxhighlight lang="vbscript">
<lang VBScript>
' Read the list of paths (newline-separated) into an array...
strPaths = Split(WScript.StdIn.ReadAll, vbCrLf)
Line 2,947:
' Remove the final "/"...
WScript.Echo Left(strPath, Len(strPath) - 1)
</syntaxhighlight>
</lang>
 
=={{header|Visual Basic}}==
Line 2,954:
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
<langsyntaxhighlight lang="vb">Public Function CommonDirectoryPath(ParamArray Paths()) As String
Dim v As Variant
Dim Path() As String, s As String
Line 3,013:
"/"
 
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var findCommonDir = Fn.new { |paths, sep|
var count = paths.count
if (count == 0) return ""
Line 3,045:
]
System.write("The common directory path is: ")
System.print(findCommonDir.call(paths, "/"))</langsyntaxhighlight>
 
{{out}}
Line 3,054:
=={{header|Yabasic}}==
{{trans|GW-BASIC}}
<langsyntaxhighlight lang="yabasic">x$ = "/home/user1/tmp/coverage/test"
y$ = "/home/user1/tmp/covert/operator"
z$ = "/home/user1/tmp/coven/members"
Line 3,079:
REM Task description says no trailing slash, so...
a = i - 1
print "Common path is '", left$(x$, a), "'"</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">dirs:=T("/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator",
"/home/user1/tmp/coven/members");
n:=Utils.zipWith('==,dirs.xplode()).find(False); // character pos which differs
n=dirs[0][0,n].rfind("/"); // find last "/"
dirs[0][0,n];</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.