Sometimes a long absolute path, in e.g. a command-line parameter to a linux tool, can be shortened, using current working directory as reference:
$ pwd
/home/heh
$ cat /home/heh/mydir/myfile
my stuff
$ cat mydir/myfile
my stuff
In this challenge, you should make a function or a program that receives two parameters:
- Absolute path, using the linux format (starts with
/
) - Current directory, using the same format
The output is the shorter of the following:
- Input 1 unchanged
- Relative path that refers to the same file/directory as the absolute path
Fine points:
- If your operating system is compatible with linux, you can use the system's current directory instead of receiving it as input
- You can assume the inputs contain only alphanumeric characters (and path separators)
- You can assume the input absolute path doesn't have a path separator
/
at the end - You can assume the input current directory has a path separator
/
at the end - You cannot assume that the absolute path refers to an existing file, or that any part of it is an accessible directory; however, the current directory can be assumed valid
- You can assume there are no symlinks anywhere near either path - because I don't want to require any special way of dealing with symlinks
- No need to support the case where either of the inputs is the root directory
- "The current directory" should be output as
.
(an empty string is not valid)
Test cases (input1, input2, output):
/home/user/mydir/myfile
/home/user
mydir/myfile
/var/users/admin/secret/passwd
/var/users/joe/hack
../../admin/secret/passwd
/home/user/myfile
/tmp/someplace
/home/user/myfile
/dir1/dir2
/dir1/dir2/dir3/dir4
../..
/dir1/dir2
/dir1/dir2
.
/
at the end". However, in your examples, this is not the case. \$\endgroup\$/home/test /home/user/mydir/myfile /home/test
and/a/b /a/b/d/e /a/b
\$\endgroup\$