Created
April 4, 2018 16:06
-
-
Save diegommarino/912d7524699d3c09d0c026411ccb40e4 to your computer and use it in GitHub Desktop.
Hash table example
This file contains 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
def checkSum(arr, s) | |
hashArr = Hash[arr.each_with_index.collect { |v,i| [v,i] }] | |
puts hashArr | |
arr.each do |v| | |
return true unless hashArr[s-v].nil? | |
end | |
false | |
end | |
puts "Input the array (ex: 1,2,3,4,5)" | |
a = gets.split(',').map{ |v| v.to_i } | |
puts "Input the sum to check" | |
s = gets.to_i | |
puts checkSum(a, s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment