Skip to content

Instantly share code, notes, and snippets.

@diegommarino
Created April 4, 2018 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegommarino/912d7524699d3c09d0c026411ccb40e4 to your computer and use it in GitHub Desktop.
Save diegommarino/912d7524699d3c09d0c026411ccb40e4 to your computer and use it in GitHub Desktop.
Hash table example
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