Created
February 23, 2015 14:21
-
-
Save katiebuilds/8f1131626a88d955c097 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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. | |
# WRITE YOUR CODE HERE. Name your method `complements?`. | |
def complements?(array, sum) | |
return false if array == nil || array.length < 2 | |
difference = [] | |
array.each do |i| | |
difference << sum - i | |
end | |
difference.each do |q| | |
if array.include?(q) | |
return true | |
else | |
return false | |
end | |
end | |
end | |
# remembering now, this was the thing that had to do with the index. can visualize the excel spreadsheet and a and b doing their thing. don't remember the exact code | |
# but what i am trying to figure out now is, why this isn't passing the bigger arrays test...it is true | |
# for the first one but false for the second....WHY? irb says that difference = [-24, -78, 0, 4, -4], so why doesn't it work why why why | |
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