(RankList for this Question)
It’s Friday night and you have just clocked out of work, just then to your dismay your boss has other plans for you. An intern working has messed up a critical task assigned to him and it’s your responsibility to fix it before the error starts affecting the system and affecting the companies image on social media.
The intern had to write down a bunch of Unix operating system paths which he has written in a longer format, it is your responsibility to shorten the paths written by him but at the same time keep the directory it points to the same
Write a program that takes a non-empty string representing a valid Unix-shell path and print out a shortened version of it
Here are some rules to help you out
1)A path can be an absolute path, meaning it starts at the root directory in the file system, or a relative path meaning it starts at the current directory in a file system
2)The root directory is represented as /. This means that if a path starts with /, its an absolute path otherwise it isn’t
3)The symbol / otherwise represents the directory separator. This means the path /foo/bar is the location of the directory bar inside directory foo which is inside the root directory
4)The symbol .. represents the parent directory thus /foo/bar/.. Is equivalent to /foo
5)The symbol . represents the current directory thus /foo/bar/. Is equivalent to /foo/bar
6)The symbols / and . can be repeated sequentially without consequences;
7)The symbol .. cannot, because repeating it sequentially means going up the parent directories. For example /foo/bar/baz/./././ is equivalent to /foo/bar/baz but /foo/bar/baz/../../../ and /foo/bar/baz definitely aren't. The only exception is the root directory /../../../ and / are equivalent because the root directory does not have any parent directory
The shortened version of the path must be equivalent to the original path. The companies image is in your hands, all the best your time starts now.
Print the shortened string
This is an absolute path as it starts with /. We enter the folder foo, then next in the string is .. which leads back to the root directory. Going ahead we enter folder test which is followed by another .. leading back to the root directory. Like this, we keep reading the string to get the output /foo/bar/baz
Log In to solve the Question