summaryrefslogtreecommitdiff
path: root/spec/heaps_stacks_queues/simplify_path_spec.rb
blob: 6516d9107b34135f12fbd969c3f6441ce87df037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<<-DOC
Given an absolute file path (Unix-style), shorten it to the format /<dir1>/<dir2>/<dir3>/....

Here is some info on Unix file system paths:

/ is the root directory; the path should always start with it even if it isn't there in the given path;
/ is also used as a directory separator; for example, /code/fights denotes a fights subfolder in the code folder in the root directory;
this also means that // stands for "change the current directory to the current directory"
. is used to mark the current directory;
.. is used to mark the parent directory; if the current directory is root already, .. does nothing.
Example

For path = "/home/a/./x/../b//c/", the output should be
simplifyPath(path) = "/home/a/b/c".

Here is how this path was simplified:
* /./ means "move to the current directory" and can be replaced with a single /;
* /x/../ means "move into directory x and then return back to the parent directory", so it can replaced with a single /;
* // means "move to the current directory" and can be replaced with a single /.

Input/Output

[time limit] 4000ms (rb)
[input] string path

A line containing a path presented in Unix style format. All directories in the path are guaranteed to consist only of English letters.

Guaranteed constraints:
1 ≤ path.length ≤ 5 · 104.

[output] string

The simplified path.
DOC

describe "simplify_path" do
  def reduce(path)
    path.split('/').inject([]) do |stack, part|
      case part
      when '.', ''
      when '..'
        stack.pop
      else
        stack.push(part)
      end
      stack
    end
  end

  def simplify_path(path)
    "/#{reduce(path).join('/')}"
  end

  [
    { path: "/home/a/./x/../b//c/", x: '/home/a/b/c' },
    { path: "/a/b/c/../..", x: '/a' },
    { path: "/../", x: '/' },
    { path: "/", x: "/" },
    { path: "//a//b//./././c", x: '/a/b/c' },
    { path: "a/../../b/", x: '/b' },
    { path: "a/b/../c/d/../../e/../../", x: "/" },
    { path: "/.././///", x: "/" },
    { path: "/cHj/T//", x: "/cHj/T" },
    { path: "/////..///K/BruP/RMplU/././", x: "/K/BruP/RMplU" },
    { path: "/mpJN/..///../../ubYgf/tFM/", x: "/ubYgf/tFM" },
    { path: "/N/cKX/bdrC/./ozFyd/NyuwO/", x: "/N/cKX/bdrC/ozFyd/NyuwO" },
    { path: "/home/", x: "/home" },
    { path: "/AagbK/////iavh/M/rmKaS/tXD/././lND//", x: "/AagbK/iavh/M/rmKaS/tXD/lND" },
    { path: "/oCTY/XJwyB/zA/qgfp/RQFl/kY/./Pa/nth/", x: "/oCTY/XJwyB/zA/qgfp/RQFl/kY/Pa/nth" },
    { path: "/home//foo/", x: "/home/foo" },
    { path: "/a/./b/../../c/", x: "/c" },
    { path: "a/b/../c/d/../../e/../../a/", x: "/a" },
  ].each do |x|
    it do
      expect(simplify_path(x[:path])).to eql(x[:x])
    end
  end
end