Created
February 5, 2015 07:24
-
-
Save ajahongir/37217106c3e21e5ea5c4 to your computer and use it in GitHub Desktop.
sample test method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(x, a) | |
return 0 if a.empty? || !x.is_a?(Integer) | |
indexes = matched_indexes(a, x) | |
not_matched = a.size - indexes.size | |
k = -1 | |
return k if indexes.size <= 1 || not_matched.odd? | |
indexes.each do |ind, count| | |
if ind > count + not_matched / 2 | |
k = ind - not_matched / 2 | |
break | |
end | |
end | |
k | |
end | |
def matched_indexes(a, x) | |
result = {} | |
a.each_with_index do |item, index| | |
result[index] = result.keys.size + 1 if item == x | |
end | |
result | |
end | |
def test(result, compare_result) | |
if result == compare_result | |
print '.' #success | |
else | |
print '-' #fail | |
end | |
end | |
a = [] | |
x = 0 | |
k = 0 | |
test(solution(x, a), k) | |
a = [] | |
x = -1 | |
k = 0 | |
test(solution(x, a), k) | |
a = [1, 2, 5, 5, 2, 3, 5] | |
x = 9 | |
k = -1 | |
test(solution(x, a), k) | |
a = [1, 2, 5, 5, 2, 3, 5] | |
x = 2 | |
k = -1 | |
test(solution(x, a), k) | |
a = [5, 5, 1, 2, 2, 3, 5] | |
x = 5 | |
k = 4 | |
test(solution(x, a), k) | |
a = (1..99_999).to_a + [5] | |
x = 5 | |
k = 50_000 | |
test(solution(x, a), k) | |
a = (1..99_999).to_a | |
x = 5 | |
k = -1 | |
test(solution(x, a), k) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment