-
Notifications
You must be signed in to change notification settings - Fork 0
/
T-Demo.rb
48 lines (38 loc) · 914 Bytes
/
T-Demo.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
# incorrect, read carefully
def solution(a)
sum = a.inject(:+)
left = 0
return -1 if a.count == 0
return 0 if a.count == 1
return 0 if left == sum
(0...a.count - 1).each { |i|
left += a[i]
return i + 1 if left == sum - left - a[i + 1]
}
-1
end
require "minitest/autorun"
require "byebug"
require "timeout"
# byebug
# solution(nil)
# exit
class TestSolution < Minitest::Test
def test_default
assert_equal 1, solution([-1, 3, -4, 5, 1, -6, 2, 1] )
end
def test_simple
assert_equal 0, solution([0])
assert_equal 0, solution([1])
assert_equal 2, solution([1, 1, 1, 2])
assert_equal 2, solution([-1, 1, 1])
end
def test_small
assert_equal -1, solution([])
end
def test_large
Timeout::timeout(6) {
assert_equal 0, solution([0] * 100_000 )
}
end
end