-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT-1.rb
49 lines (42 loc) · 1.06 KB
/
T-1.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
def solution(x, a)
total_different = a.count { |v| v != x }
different = 0
equal = 0
(0...a.count).each { |k|
return k if equal == total_different - different
if a[k] == x
equal += 1
else
different += 1
end
}
-1
end
require "minitest/autorun"
require "byebug"
require "timeout"
# byebug
# solution(nil)
# exit
class TestSolution < Minitest::Test
def test_default
assert_equal 4, solution(5, [5, 5, 1, 7, 2, 3, 5])
end
def test_simple
assert_equal 1, solution(3, [3, 5])
assert_equal 3, solution(3, [1, 3, 5, 1])
assert_equal 3, solution(3, [1, 2, 5, 3])
assert_equal -1, solution(3, [5, 5, 5])
assert_equal 0, solution(5, [5, 5, 5])
end
def test_small
assert_equal 0, solution(5, [5])
assert_equal -1, solution(5, [6])
end
def test_large
Timeout::timeout(6) {
assert_equal 0, solution(1, [1] * 100_000 )
assert_equal -1, solution(2, [1] * 100_000 )
}
end
end