Skip to content

Instantly share code, notes, and snippets.

@kinduff
Created July 29, 2015 19:00
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 kinduff/771b3d9f6d9d63620d8c to your computer and use it in GitHub Desktop.
Save kinduff/771b3d9f6d9d63620d8c to your computer and use it in GitHub Desktop.
class HashTable
def initialize(size)
@size = size
@buckets = Array.new(@size)
end
def add(value)
index = hash(value)
@buckets[index] = value
end
def hash(value)
sum = 0
0.upto(value.size-1) do |i|
sum += value[i].ord - 97
end
return sum % @size
end
end
hash = HashTable.new(3)
# => [nil, nil, nil]
hash.add('fear')
# => [nil, nil, "fear"]
hash.add('is the')
# => ["is the", nil, "fear"]
hash.add('little death')
# => ["is the", "little death", "fear"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment