master
 1<<-DOC
 2Given an absolute file path (Unix-style), shorten it to the format /<dir1>/<dir2>/<dir3>/....
 3
 4Here is some info on Unix file system paths:
 5
 6/ is the root directory; the path should always start with it even if it isn't there in the given path;
 7/ is also used as a directory separator; for example, /code/fights denotes a fights subfolder in the code folder in the root directory;
 8this also means that // stands for "change the current directory to the current directory"
 9. is used to mark the current directory;
10.. is used to mark the parent directory; if the current directory is root already, .. does nothing.
11Example
12
13For path = "/home/a/./x/../b//c/", the output should be
14simplifyPath(path) = "/home/a/b/c".
15
16Here is how this path was simplified:
17* /./ means "move to the current directory" and can be replaced with a single /;
18* /x/../ means "move into directory x and then return back to the parent directory", so it can replaced with a single /;
19* // means "move to the current directory" and can be replaced with a single /.
20
21Input/Output
22
23[time limit] 4000ms (rb)
24[input] string path
25
26A line containing a path presented in Unix style format. All directories in the path are guaranteed to consist only of English letters.
27
28Guaranteed constraints:
291  path.length  5 · 104.
30
31[output] string
32
33The simplified path.
34DOC
35
36describe "simplify_path" do
37  def reduce(path)
38    path.split('/').inject([]) do |stack, part|
39      case part
40      when '.', ''
41      when '..'
42        stack.pop
43      else
44        stack.push(part)
45      end
46      stack
47    end
48  end
49
50  def simplify_path(path)
51    "/#{reduce(path).join('/')}"
52  end
53
54  [
55    { path: "/home/a/./x/../b//c/", x: '/home/a/b/c' },
56    { path: "/a/b/c/../..", x: '/a' },
57    { path: "/../", x: '/' },
58    { path: "/", x: "/" },
59    { path: "//a//b//./././c", x: '/a/b/c' },
60    { path: "a/../../b/", x: '/b' },
61    { path: "a/b/../c/d/../../e/../../", x: "/" },
62    { path: "/.././///", x: "/" },
63    { path: "/cHj/T//", x: "/cHj/T" },
64    { path: "/////..///K/BruP/RMplU/././", x: "/K/BruP/RMplU" },
65    { path: "/mpJN/..///../../ubYgf/tFM/", x: "/ubYgf/tFM" },
66    { path: "/N/cKX/bdrC/./ozFyd/NyuwO/", x: "/N/cKX/bdrC/ozFyd/NyuwO" },
67    { path: "/home/", x: "/home" },
68    { path: "/AagbK/////iavh/M/rmKaS/tXD/././lND//", x: "/AagbK/iavh/M/rmKaS/tXD/lND" },
69    { path: "/oCTY/XJwyB/zA/qgfp/RQFl/kY/./Pa/nth/", x: "/oCTY/XJwyB/zA/qgfp/RQFl/kY/Pa/nth" },
70    { path: "/home//foo/", x: "/home/foo" },
71    { path: "/a/./b/../../c/", x: "/c" },
72    { path: "a/b/../c/d/../../e/../../a/", x: "/a" },
73  ].each do |x|
74    it do
75      expect(simplify_path(x[:path])).to eql(x[:x])
76    end
77  end
78end