Skip to content

Instantly share code, notes, and snippets.

@alvinlai
Forked from sranso/hash-table.rb
Last active August 29, 2015 14:15
Show Gist options
  • Save alvinlai/88f33e13cf688261193d to your computer and use it in GitHub Desktop.
Save alvinlai/88f33e13cf688261193d to your computer and use it in GitHub Desktop.
class MyHash
attr_reader :buckets
def initialize
@buckets = []
end
def assign_or_find_index(k)
special_function(k) % 100
end
def special_function(k)
k = k.to_sym
k.object_id
end
def insert(k, v)
index = assign_or_find_index(k)
buckets[index] ||= []
buckets[index] << [k, v]
end
def find(k)
index = assign_or_find_index(k)
buckets[index].each do |k_v_pair|
if k_v_pair[0] == k
return k_v_pair[1]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment