Created
July 29, 2015 19:00
-
-
Save kinduff/771b3d9f6d9d63620d8c to your computer and use it in GitHub Desktop.
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
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