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
|
#! /usr/bin/ruby
def lcs(*ns)
l=ns.sort
w=Array.new
r=Array.new
while (n=l.shift)
if (w.length==0 or n==w[-1]+1)
w.push(n)
else
if (w.length > r.length)
r=w
end
w=[n]
end
end
if (w.length > r.length)
r=w
end
if (r.length > 1)
return r
else
return 0
end
end
require 'test/unit'
class TestLcs < Test::Unit::TestCase
def test_ex1
assert_equal([2,3,4],lcs(100, 4, 50, 3, 2))
end
def test_ex2
assert_equal(0,lcs(20, 30, 10, 40, 50))
end
def test_ex3
assert_equal([9,10,11],lcs(20, 19, 9, 11, 10))
end
def test_ex4
assert_equal([18,19,20],lcs(20, 19, 18, 11, 10))
end
end
|