Skip to content

Instantly share code, notes, and snippets.

@RedSoxFan22
Created June 25, 2015 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedSoxFan22/90ba9e092b57e65f1e7b to your computer and use it in GitHub Desktop.
Save RedSoxFan22/90ba9e092b57e65f1e7b to your computer and use it in GitHub Desktop.
complements?
require 'minitest/autorun'
require 'minitest/pride'
# Write a method which accepts an array as the first paramater and a number
# as the second parameter. Return true if two of the numbers in the array sum
# to the second parameter.
def complements(array, num)
if array.length < 2 || array.length == nil
false
elsif
new_array = array.select[|x| x <= num]
new_array.inject (:+) until == num #put that sum into array,
# count the items in the array
else
end
end
class DoubleLoopChallenge < MiniTest::Test
def test_exact_match_arrays
assert complements?([1, 0], 1)
assert complements?([-4, 4], 0)
assert complements?([25, 43], 68)
refute complements?([25, 25], 25)
refute complements?([1, 3], 25)
refute complements?([-25, 25], 25)
end
def test_too_small_arrays
refute complements?([25], 25)
refute complements?([], 25)
refute complements?(nil, 25)
end
def test_bigger_arrays
assert complements?([1, 0, 2, 17, 8], 1)
assert complements?([24, 78, 0, -4, 4], 0)
assert complements?([1, 25, 3, 8, -8, 43], 68)
refute complements?([18, 25, 43, 25, 98, 10], 25)
refute complements?([12, 2, 3, 9, 11], 25)
refute complements?([25, -25, 25, -25, 25], 25)
end
def test_double_counting
refute complements?([12, 0], 24)
refute complements?([50, -10], 100)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment