How to resolve the algorithm Find common directory path step by step in the Oz programming language

Published on 12 May 2024 09:40 PM
#Oz

How to resolve the algorithm Find common directory path step by step in the Oz programming language

Table of Contents

Problem Statement

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: Note: The resultant path should be the valid directory '/home/user1/tmp' and not the longest common string '/home/user1/tmp/cove'. 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find common directory path step by step in the Oz programming language

Source code in the oz programming language

declare
  fun {CommonPrefix Sep Paths}
     fun {GetParts P} {String.tokens P Sep} end
     Parts = {ZipN {Map Paths GetParts}}
     EqualParts = {List.takeWhile Parts fun {$ X|Xr} {All Xr {Equals X}} end}
  in
     {Join Sep {Map EqualParts Head}}
  end

  fun {ZipN Xs}
     if {Some Xs {Equals nil}} then nil
     else
        {Map Xs Head} | {ZipN {Map Xs Tail}}
     end
  end

  fun {Join Sep Xs}
     {FoldR Xs fun {$ X Z} {Append X Sep|Z} end nil}
  end

  fun {Equals C}
     fun {$ X} X == C end
  end

  fun {Head X|_} X end

  fun {Tail _|Xr} Xr end
in
  {System.showInfo {CommonPrefix &/
                    ["/home/user1/tmp/coverage/test"
                     "/home/user1/tmp/covert/operator"
                     "/home/user1/tmp/coven/members"]}}

  

You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the Prolog programming language
You may also check:How to resolve the algorithm Repeat step by step in the Oforth programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the Java programming language
You may also check:How to resolve the algorithm Create a file step by step in the friendly interactive shell programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Nim programming language