Find common directory path: Difference between revisions

no edit summary
(Added AppleScript.)
No edit summary
(11 intermediate revisions by 7 users not shown)
Line 750:
{{out}}
<pre>/home/user1/tmp</pre>
 
=={{header|EasyLang}}==
{{trans|AWK}}
 
<syntaxhighlight lang=easylang>
func$ comdir path$[] .
for i = 1 to len path$[1]
c$ = substr path$[1] i 1
for j = 2 to len path$[]
if c$ <> substr path$[j] i 1
break 2
.
.
if c$ = "/"
f = i - 1
.
.
return substr path$[1] 1 f
.
a$[] &= "/home/user1/tmp/coverage/test"
a$[] &= "/home/user1/tmp/covert/operator"
a$[] &= "/home/user1/tmp/coven/members"
print comdir a$[]
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 1,703 ⟶ 1,727:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec common_prefix xs ys = match xs, ys with
| x :: xs, y :: ys when x = y -> x :: common_prefix xs ys
| _ -> []
 
let common_prefix_all = function
<syntaxhighlight lang="ocaml">let rec aux acc paths =
| x :: xs -> List.fold_left common_prefix x xs
if List.mem [] paths
| _ -> []
then (List.rev acc) else
let heads = List.map List.hd paths in
let item = List.hd heads in
let all_the_same =
List.for_all ((=) item) (List.tl heads)
in
if all_the_same
then aux (item::acc) (List.map List.tl paths)
else (List.rev acc)
 
let common_prefixcommon_ancestor ~sep =paths function=
List.map Str.(split_delim (regexp_string sep)) paths
| [] -> invalid_arg "common_prefix"
|> common_prefix_all
| dirs ->
|> String.concat sep
let paths = List.map (Str.split (Str.regexp_string sep)) dirs in
let res = aux [] paths in
(sep ^ (String.concat sep res))
 
let ()_ = assert begin
common_ancestor ~sep:"/" [
let dirs = [
"/home/user1/tmp/coverage/test";
"/home/user1/tmp/covert/operator";
"/home/user1/tmp/coven/members";
] = "/home/user1/tmp"
] in
end</syntaxhighlight>
print_endline (common_prefix "/" dirs);
;;</syntaxhighlight>
 
(usesRequires the modulestandard <code>[httphttps://camlocaml.inria.fr/pub/docsorg/manual-ocaml/libref/Strlibstr.html Strstr]</code>, str.cma)library
 
=={{header|OpenEdge/Progress}}==
Line 1,834 ⟶ 1,850:
};
cdp(["/home/user1/tmp/coverage/test","/home/user1/tmp/covert/operator","/home/user1/tmp/coven/members"])</syntaxhighlight>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Program CommonPaths;
{$mode ObjFPC}{$H+}
uses
Classes, Math;
 
const
Paths: array of string = ('/home/user1/tmp/coverage/test',
'/home/user1/tmp/covert/operator',
'/home/user1/tmp/coven/members');
 
function FindShortestCommonPath(arr: array of TStringList; shortestPath: Integer): string;
var
i, j: Integer;
commonStr: string;
begin
Result := '/';
if Length(arr) = 0 then
Exit;
for j := 0 to shortestPath - 1 do
begin
commonStr := arr[0][j];
for i := 1 to High(arr) do
begin
if arr[i][j] <> commonStr then
Exit(Result);
end;
Result := Result + commonStr + '/';
end;
end;
var
arr: array of TStringList;
i, shortestpath: uint32;
 
begin
shortestpath := High(uint32);
SetLength(arr, Length(paths));
 
for i := 0 to High(paths) do
begin
arr[i] := TStringList.Create;
arr[i].AddDelimitedText(paths[i], '/', false);
arr[i].Delete(0);
shortestpath := Min(shortestpath, arr[i].Count);
end;
 
Writeln(FindShortestCommonPath(arr, shortestpath));
 
for i := 0 to High(paths) do
arr[i].Free;
end.
</syntaxhighlight>
{{out}}
<pre>
/home/user1/tmp/
</pre>
 
 
 
 
=={{header|Perl}}==
Line 2,467 ⟶ 2,545:
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program finds the common directory path for a list of files. */
/* original code: Gerard Schildberger @. = /*the default for all file lists (null)*/
/* 20230606 Walter Pachl refurbisher adn improved (file.4 = 'home' -> /) */
@.1 = '/home/user1/tmp/coverage/test'
file. = '' /*the default for all file lists (null)*/
@.2 = '/home/user1/tmp/covert/operator'
@file.31 = '/home/user1/tmp/covencoverage/memberstest'
/*123456789.123456789.123456768*/
L= length(@.1) /*use the length of the first string. */
file.2 = '/home/user1/tmp/covert/operator'
do j=2 while @.j\=='' /*start search with the second string. */
file.3 = '/home/user1/tmp/coven/members'
_= compare(@.j, @.1) /*use REXX compare BIF for comparison*/
if _==0 then iterate /*Strings are equal? Then con't use min*/
L= min(L, _) /*get the minimum length equal strings.*/
if right(@.j, 1)=='/' then iterate /*if a directory, then it's OK. */
L= lastpos('/', left(@.j, L) ) /*obtain directory name up to here*/
end /*j*/
 
commonL= leftlength( @file.1,) lastpos('/', @.1, L) ) /*determineuse the shortestlength of DIRthe first string. */
Do j=2 While file.j\=='' /*loop for the other file names */
if right(common, 1)=='/' then common= left(common, max(0, length(common) - 1) )
diffp=compare(file.j,file.1) /*find the first different character */
if common=='' then common= "/" /*if no common directory, assume home. */
say 'common directoryIf diffp>0 Then Do path: ' common /*Strings are different [↑] handle trailing / delimiter*/
L=min(L,diffp) /*stickget athe forkminimum inlength it,equal we're all donestrings. */</syntaxhighlight>
If right(file.j,1)<>'/' Then Do /*not a directory */
L=lastpos('/',left(file.j,L)) /* go back to directory end */
If L=0 Then Do
Say 'common directory path: /'
Exit
End
End
End
End
common=left(file.1,lastpos('/',file.1,L)) /*determine the shortest DIR string.*/
If right(common,1)=='/' Then /* remove the trailing / */
common=left(common,length(common)-1)
If common=='' then common= "/" /*if no common directory, assume home. */
Say 'common directory path: 'common
/*stick a fork in it, we're all done. */
</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,521 ⟶ 2,610:
<pre>
/home/user1/tmp
</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
≪ DUP SIZE → paths n
≪ paths n GET "/" +
'''WHILE''' 'n' DECR '''REPEAT'''
paths n GET "/" +
DUP2 SIZE SWAP SIZE MIN DUP
1 SWAP '''FOR''' j
DROP
OVER j DUP SUB OVER j DUP SUB ≠
j
'''IF''' SWAP '''THEN''' DUP 1 ≠ - OVER SIZE 'j' STO '''END'''
'''NEXT'''
1 SWAP SUB SWAP DROP
'''END'''
DUP SIZE
'''WHILE''' DUP2 DUP SUB "/" ≠ '''REPEAT''' 1 - '''END'''
DUP 1 ≠ -
1 SWAP SUB
≫ ≫ '<span style="color:blue">CPATH</span>' STO
 
{ "/home/user1/tmp/coverage/test" "/home/user1/tmp/covert/operator" "/home/user1/tmp/coven/members" } <span style="color:blue">CPATH</span>
{{out}}
<pre>
1: "/home/user1/tmp"
</pre>
 
Line 2,914 ⟶ 3,030:
var output:String = getPrefix(test)!
print(output)</syntaxhighlight>
 
===Works on MacOS===
<syntaxhighlight lang="swift">
import Foundation
 
func commonPrefix<T: Equatable>(_ lhs: [T], _ rhs: [T]) -> [T] {
for tryLen in (0...min(lhs.count,rhs.count)).reversed() {
if lhs.starts(with: rhs.prefix(tryLen)) {
return Array<T>(rhs.prefix(tryLen))
}
}
return []
}
 
var test = ["/home/user1/tmp/coverage/test",
"/home/user1/tmp/covert/operator",
"/home/user1/tmp/coven/members"]
 
let lcp: String = test.reduce("") { lhs, rhs in
if !lhs.isEmpty {
var commonSoFar = commonPrefix(
lhs.components(separatedBy: "/"),
rhs.components(separatedBy: "/")
)
return commonSoFar.joined(separator: "/")
}
return rhs
}
print("Longest common path: \(lcp)")
 
// Longest common path: /home/user1/tmp
</syntaxhighlight>
 
=={{header|Tcl}}==
Line 3,120 ⟶ 3,268:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var findCommonDir = Fn.new { |paths, sep|
var count = paths.count
if (count == 0) return ""
45

edits