Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -7,3 +7,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:'''''
* [[Longest common prefix]]
<br>

View file

@ -0,0 +1,11 @@
defmodule RC do
def common_directory_path(dirs, separator \\ "/") do
dir1 = Enum.min(dirs) |> String.split(separator)
dir2 = Enum.max(dirs) |> String.split(separator)
Enum.zip(dir1,dir2) |> Enum.take_while(fn {a,b} -> a==b end)
|> Enum.map_join(separator, fn {a,a} -> a end)
end
end
dirs = ~w( /home/user1/tmp/coverage/test /home/user1/tmp/covert/operator /home/user1/tmp/coven/members )
IO.inspect RC.common_directory_path(dirs)

View file

@ -0,0 +1,27 @@
function commonpath{T<:String}(ds::Array{T,1}, delim::Char='/')
0 < length(ds) || return convert(T, "")
1 < length(ds) || return ds[1]
p = split(ds[1], delim)
mcnt = length(p)
for d in ds[2:end]
q = split(d, delim)
mcnt = min(mcnt, length(q))
hits = findfirst(p[1:mcnt] .== q[1:mcnt], false)
hits != 0 || continue
mcnt = hits - 1
mcnt != 0 || return convert(T, "")
end
1 < mcnt || p[1] != "" || return convert(T, string(delim))
convert(T, join(p[1:mcnt], delim))
end
test = ["/home/user1/tmp/coverage/test",
"/home/user1/tmp/covert/operator",
"/home/user1/tmp/coven/members"]
println("Comparing")
for s in test
println(" ", s)
end
println("for their common directory path yields:")
println(" ", commonpath(test))

View file

@ -0,0 +1,14 @@
Function Get-CommonPath( $Separator, $PathList ){
$SplitPaths = $PathList | foreach { , $_.Split($Separator) }
$MinDirectoryDepth = $SplitPaths | Measure-Object -Property Length -Minimum | Select -ExpandProperty Minimum
$CommonPath = foreach ($Index in 0..($MinDirectoryDepth - 1)) {
$UniquePath = @($SplitPaths | foreach { $_[$Index] } | Sort -Unique)
if ($UniquePath.Length -gt 1) {
break;
}
$UniquePath
}
[String]::Join($Separator, $CommonPath)
}

View file

@ -0,0 +1,2 @@
PS> Get-CommonPath '/' "/home/user1/tmp/coverage/test","/home/user1/tmp/covert/operator","/home/user1/tmp/coven/members"
/home/user1/tmp

View file

@ -1,5 +1,5 @@
>>> def commonprefix(*args, sep='/'):
return os.path.commonprefix(*args).rpartition(sep)[0]
>>> 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'])

View file

@ -1,5 +1,5 @@
separator = '/'
paths = dirs.collect {|dir| dir.split(separator)}
uncommon_idx = paths[0].zip(*paths[1..-1]).index {|dirnames| dirnames.uniq.length > 1}
uncommon_idx = paths[0].length unless uncommon_idx # if uncommon_idx==nil
common_directory = paths[0][0...uncommon_idx].join(separator) # => "/home/user1/tmp"
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"

View file

@ -0,0 +1,6 @@
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"