Skip to content

Instantly share code, notes, and snippets.

@ajahongir
Created February 5, 2015 07:24
Show Gist options
  • Save ajahongir/37217106c3e21e5ea5c4 to your computer and use it in GitHub Desktop.
Save ajahongir/37217106c3e21e5ea5c4 to your computer and use it in GitHub Desktop.
sample test method
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