Created
February 25, 2019 18:53
-
-
Save coreyjs/6e37f4c89760cefde2cb192b0c424058 to your computer and use it in GitHub Desktop.
Daily Coding Email Challenge 2
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
| puts 'Daily Coding Challenge 1' | |
| puts 'Given a list of numbers and a number k, return whether any two numbers from the list add up to k.' | |
| input = [10, 15, 3, 7] | |
| k = 17 | |
| puts "input = #{input.to_s}" | |
| puts "k = #{k}" | |
| input.each_with_index do |num, i| | |
| # remainder is the number we want to find exists in the array | |
| remainder = k - num | |
| if input.include?(remainder) and input.index(remainder) != i | |
| puts 'Found Answer: ' | |
| puts "#{num} (index #{i}) + #{remainder} (index #{input.index(remainder)}) == #{k}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment