Messy Kevin III
Since Kevin from the IT team had been struggling to deal with the log files, his manager decided to put him on another task. Kevin's new task was to index the files on the Linux servers of his company. To do so, he would need to take each of the file paths and print them out into a file.
Sounds easy, right? Not for Kevin it seems, as he found another way to mess things up. Instead of the file paths being all clean like /home/kevin/homework/maths/assignment1.pdf
, his paths were more like /home///kevin/./homework/english/../maths/assignment1.pdf
. What a mess!
You've been called in to clean up the mess and output the file paths properly. Kevin's counting on you (mostly because he doesn't know how to fix it himself).
Input
The first line consists of an integer , the number of file paths you must fix.
The next lines contains a string
, representing the file path.
Only characters in the paths are
.
, /
and alphanumeric characters.
You are given a few notes about the properties of these file paths:
- Paths are Unix file paths, meaning they use forward slashes (
/
) to denote directories. - A single dot (
.
) denotes the current directory, while two dots (..
) denotes the parent directory. These denote relative directories. - A path that starts with
/
means the path starts from the root directory. - Multiple adjacent forward slashes will be treated as one (e.g.
foo////bar
isfoo/bar
). - A path like
foo/bar/../baz
will be converted tofoo/baz
since..
tells the path to go up one level frombar
tofoo
, then we go to thebaz
folder.
Output
Your job is to normalise the paths by removing any unnecessary slashes and relative directory symbols (.
and ..
). Each line should contain the path but normalised.
Clarifications
You can also assume that:
- The directories will not have trailing slashes, so a path like
foo/bar/
will not be given. - Shell expansions like
~
and$
will not be used. - You will never go up to a directory beyond the root (
/
) so/foo/../..
will not happen.
Example
Input 1
1
foo/bar/../baz
Output 1
foo/baz
Since we have ..
, we go up to the parent directory of bar
which is foo
, to get the final path of foo/baz
.
Input 2
1
logs/./2025///july
Output 2
logs/2025/july
The current directory ./
is redundant, while the extra slashes (///
) get converted into a single slash.
Input 3
1
///home/kevin/logs/../pathconverter.py
Output 3
/home/kevin/pathconverter.py
Comments