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

def cc(list)
  n=(0...list.length).sort_by {|x| list[x]}
  k=Array.new(list.length) {0}
  for i in n
    nr=[1]
    if i > 0 && list[i-1] < list[i]
      nr.push(k[i-1]+1)
    end
    if i < list.length-1 && list[i+1] < list[i]
      nr.push(k[i+1]+1)
    end
    k[i]=nr.max
  end
  return k.sum
end

require 'test/unit'

class TestCc < Test::Unit::TestCase

  def test_1
    assert_equal(4,cc([1,2,2]))
  end

  def test_2
    assert_equal(7,cc([1,4,3,2]))
  end

end