Guest User

Untitled

a guest
Dec 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. # Define a method that returns a boolean indicating whether any two elements in the argument (an array) sum to 0.
  2.  
  3. def two_sum_to_zero?(arr)
  4. i = 0
  5. while i < arr.length #thereby iterating through the array
  6. num_one = arr[i]
  7. j=i+1
  8. while j <arr.length
  9. num_two = arr[j]
  10. if num_one + num_two == 0
  11. return true
  12. else
  13. return false
  14. end
  15. j=j+1
  16. end
  17. i=i+1
  18. end
  19. end
  20.  
  21.  
  22. puts two_sum_to_zero?([1,-1])
  23. puts two_sum_to_zero?([1,2,3,4])
Add Comment
Please, Sign In to add comment