Find common directory path: Difference between revisions

m
Undo revision 78197 by Dkf (Talk) whoops; collided with other edits!
m (improve wording of task description)
m (Undo revision 78197 by Dkf (Talk) whoops; collided with other edits!)
Line 1:
{{task}}
Create a routine that, given a set of strings representing directory paths and a single character directory separator, will; 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:
Line 25:
 
=={{header|PureBasic}}==
 
{{incorrect|PureBasic|Although there are only three paths in the example the task asks for a routine that works on an arbitrary set of paths, and the separator should also be an argument to the routine}}
PureBasic don't have a path comparator directly but instead have powerful string tools.
 
Simply by checking the catalog names until they mismatch and add up the correct parts, the task is accomplished.
<lang PureBasic>Procedure.s CommonPath(Array InPaths.s(1),separator.s="/")
<lang PureBasic>S1$="/home/user1/tmp/coverage/test"
Protected SOut$=""
S2$="/home/user1/tmp/covert/operator"
Protected i, j, toggle
S3$="/home/user1/tmp/coven/members"
SOut$=""
If ArraySize(InPaths())=0
i=1
ProcedureReturn InPaths(0) ; Special case, only one path
EndIf
Repeat
i+1
toggle=#False
For j=1 To ArraySize(InPaths())
If (StringField(InPaths(j-1),i,separator)=StringField(InPaths(j),i,separator))
If Not toggle
SOut$+StringField(InPaths(j-1),i,separator)+separator
toggle=#True
EndIf
Else
ProcedureReturn SOut$
EndIf
Next
ForEver
EndProcedure</lang>
 
Example of implementation
<lang PureBasic>Dim t.s(2)
<lang PureBasic>S1$t(0)="/home/user1/tmp/coverage/test"
S2$t(1)="/home/user1/tmp/covert/operator"
S3$t(2)="/home/user1/tmp/coven/members"
 
Debug CommonPath(t(),"/"))</lang>
While (StringField(S1$,i,"/")=StringField(S2$,i,"/")) And (StringField(S1$,i,"/")=StringField(S3$,i,"/"))
SOut$+StringField(S1$,i,"/")+"/"
i+1
Wend
</lang>
 
=={{header|Python}}==
Anonymous user