Skip to content

Instantly share code, notes, and snippets.

@aharpole
Created February 10, 2014 21:28
Show Gist options
  • Save aharpole/8924519 to your computer and use it in GitHub Desktop.
Save aharpole/8924519 to your computer and use it in GitHub Desktop.
Fullscreen candidate Ruby exercise
require "minitest/autorun"
#To run the test, make sure you have the minitest gem installed and just type ruby test_missing_integer.rb at the prompt
#after downloading this file.
#To submit the exercise, put the code into a gist and share with us.
#this method takes an array with 99 elements. The elements are the integers 1 through 100, but one is missing.
#The elements aren't sorted in any particular order.
#This method returns the number that's missing.
#Write the most efficient possible version of this.
def missing_integer(arr)
nil #replace this with an actual implementation.
end
class TestMissingInteger < Minitest::Test
def test_that_correct_integer_is_returned
assert_equal missing_integer([18, 39, 98, 4, 17, 79, 93, 3, 94, 74, 88, 43, 81, 8, 68, 65, 16, 20, 19, 29, 91, 6, 32, 42, 90, 69, 82, 59, 70, 86, 2, 97, 76, 77, 44, 24, 37, 95, 58, 11, 60, 28, 12, 15, 89, 40, 87, 96, 35, 22, 26, 10, 100, 46, 56, 34, 61, 72, 47, 67, 21, 51, 7, 54, 31, 50, 83, 49, 55, 57, 71, 38, 92, 13, 41, 1, 45, 84, 36, 62, 25, 80, 33, 78, 30, 5, 23, 75, 99, 85, 52, 27, 9, 48, 73, 64, 63, 14, 53] ), 66
end
def test_that_correct_integer_is_returned_with_sorted_array
assert_equal missing_integer([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, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]), 66
end
def test_that_it_isnt_just_returning_66
assert_equal missing_integer([75, 25, 80, 11, 22, 73, 74, 97, 29, 76, 44, 39, 57, 32, 23, 70, 9, 99, 4, 3, 55, 71, 36, 54, 18, 20, 48, 34, 64, 17, 35, 87, 91, 24, 46, 68, 16, 15, 93, 62, 92, 56, 26, 38, 45, 1, 31, 81, 33, 37, 8, 7, 90, 13, 51, 72, 40, 41, 82, 58, 43, 6, 100, 96, 77, 79, 98, 53, 42, 12, 27, 65, 69, 94, 60, 86, 67, 89, 78, 21, 83, 61, 50, 59, 84, 2, 52, 63, 49, 88, 14, 47, 30, 85, 10, 95, 28, 66, 19]), 5
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment