Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shaselton/56204ad22c5c6c9d8f025dc556df1368 to your computer and use it in GitHub Desktop.
Save shaselton/56204ad22c5c6c9d8f025dc556df1368 to your computer and use it in GitHub Desktop.
# Bob is preparing to pass IQ test. The most frequent task in this test is
# to find out which one of the given numbers differs from the others. Bob
# observed that one number usually differs from the others in evenness.
# Help Bob — to check his answers, he needs a program that among the given
# numbers finds one that is different in evenness, and return a position of this number.
# ! Keep in mind that your task is to help Bob solve a real IQ test, which
# means indexes of the elements start from 1 (not 0)
# Examples :
# iq_test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even
# iq_test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd
def iq_test(number_sequence)
number_sequence.split(' ').map(&:to_i).each_with_index.inject({even: [], odd: []}) do |inject_hash, element_pair|
element, index = element_pair
inject_hash[:even].push(index + 1) if element.even?
inject_hash[:odd].push(index + 1) if element.odd?
return inject_hash[:even].pop if inject_hash[:odd].size >= 2 && inject_hash[:even].size == 1
return inject_hash[:odd].pop if inject_hash[:even].size >= 2 && inject_hash[:odd].size == 1
inject_hash
end
end
puts iq_test("2 4 7 8 10")
puts iq_test("1 2 1 1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment