aboutsummaryrefslogtreecommitdiff
path: root/challenge-182/roger-bell-west/ruby/ch-2.rb
blob: 43ffba95e5cfd78d199aff923f619c173d292aa5 (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
#! /usr/bin/ruby

require 'test/unit'

def commonpath(p)
  pa = []
  pl = []
  p.each do |sp|
    q = sp.split("/")
    pl.push(q.length)
    pa.push(q)
  end
  out = []
  0.upto(pl.min-1).each do |cl|
    ex = false
    tx = pa[0][cl]
    pa.each do |pe|
      if pe[cl] != tx then
        ex = true
        break
      end
    end
    if ex then
      break
    end
    out.push(tx)
  end
  return out.join("/")
end

class TestCommonpath < Test::Unit::TestCase

  def test_ex1
    assert_equal("/a/b/c", commonpath([
                                        "/a/b/c/1/x.pl",
                                        "/a/b/c/d/e/2/x.pl",
                                        "/a/b/c/d/3/x.pl",
                                        "/a/b/c/4/x.pl",
                                        "/a/b/c/d/5/x.pl"
                                      ]))
  end

end