aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/roger-bell-west/ruby/ch-1.rb
blob: 2154ed2189cb3baca473e0b5a3851ae4127d9c77 (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
#! /usr/bin/ruby

def concat(a0, b0)
  if (b0 == 0) then
    return 10 * a0
  end
  a = a0
  b = b0
  while (b > 0) do
    a *= 10;
    b = (b / 10).floor
  end
  return a + b0
end

def concatenationvalue(a)
  t = 0
  0.upto(((a.length - 1) / 2).floor) do |i|
    j = a.length - 1 - i
    if (j == i) then
      t += a[i]
    else
      t += concat(a[i], a[j])
    end
  end
  return t
end

require 'test/unit'

class TestConcatenationvalue < Test::Unit::TestCase

  def test_ex1
    assert_equal(1286, concatenationvalue([6, 12, 25, 1]))
  end

  def test_ex2
    assert_equal(489, concatenationvalue([10, 7, 31, 5, 2, 2]))
  end

  def test_ex3
    assert_equal(112, concatenationvalue([1, 2, 10]))
  end

end