jumpstart 1 hw two sum
Define a method, #two_sum, that accepts an array and a target sum (integer) as arguments.
The method should return true if any two integers in the array sum to the target. Otherwise, it should return false. Assume the array will only contain integers.
def two_sum(array, target)
o_count = 0
while o_count < array.length
i_count = o_count + 1
while i_count < array.length
test_sum = array[o_count] + array[i_count]
if test_sum == target
return true
end
i_count += 1
end
o_count += 1
end
false
end
The explicit return is necessary when you want to break out and stop the rest of the method execution immediately.
Another way to solve it is using enumerables; the each method takes the place of the outer loop and our while loop counter is set by identifying the index of the element we're using to make test summations.
def two_sum(array, target)
array.each do |integer|
count = array.index(integer)
while count < (array.length - 1)
all_other_integers = array[count + 1].to_i
test_sum = integer + all_other_integers
if test_sum == target
return true
end
count += 1
end
end
false
end