-
Notifications
You must be signed in to change notification settings - Fork 0
/
S-TapeEquilibrium.rb
65 lines (56 loc) · 1.28 KB
/
S-TapeEquilibrium.rb
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
def solution(a)
first = 0
second = a.inject(:+)
(1..a.count - 1).map do |p|
first += a[p - 1]
second -= a[p - 1]
(first - second).abs
end.min
end
require "minitest/autorun"
require "byebug"
require "timeout"
class TestSolution < Minitest::Test
def test_default
input = [3, 1, 2, 4, 3]
output = 1
assert_equal output, solution(input)
end
def test_simple_1
input = [2, 4]
output = 2
assert_equal output, solution(input)
end
def test_simple_2
input = [2, 2]
output = 0
assert_equal output, solution(input)
end
def test_simple_3
input = [0, 2]
output = 2
assert_equal output, solution(input)
end
def test_simple_4
input = [-2, 2, 4]
output = 4
assert_equal output, solution(input)
end
def test_simple_5
input = [-1, 1]
output = 2
assert_equal output, solution(input)
end
def test_negative
input = [-1, -2, -3, -4]
output = 2
assert_equal output, solution(input)
end
def test_large
input = [1000] * 100_000
output = 0
Timeout::timeout(6) {
assert_equal output, solution(input)
}
end
end