Questions about anything?
Midterm projects are due Thursday. I'd also like to do some discussion during class that day, so be ready to do some show'n'tell.
Also, we should leave time for an in-class SEPC conversation either today or Thursday, at the end of the class without me.
New topic: this week and continuing after the midterm break. An important data structure which we have already mentioned previously - the "hash table", also known as a "dictionary" (python) or "associative array" (javascript)
Here are a few places to read about them:
There are many variations, and most high level languages have these already as built-in syntax.
First, their API and the general idea of how they work:
hashtable = HashTable() # create one
hashtable.insert(key, value) # insert a (key, value) pair # O(1)
value = hashtable.get(key) # retrieve a value # O(1)
The defining feature of a hash table is that they are fast (both putting things in and looking this up) addressable (with a "key") storage.
Let's first remind ourselves what this looks like in a typical programming language.
d = {} # create an empty dictionary
d['cat'] = 'meow' # put in a value ('meow') at a key (i.e. location i.e.'cat')
meow = d['cat'] # fetch a value given a key
But this is also a hash table in python, though we don't usually think of it that way:
class Foo:
pass
f = Foo() # create an "empty" instance
f.cat = 'meow' # put in a value ('meow" at a key (.cat))
meow = f.cat # look up a value
Javascript, in fact, merges both of those into the same thing.
$ node # invoke a javascript interpeter
> thing = {} # create an empty "object" or "associative array" (same thing)
> thing['cat'] = 'meow' # insert (key, value) = (cat, meow)
> thing.dog = "woof" # or insert (key, value) = (dog, woof) ... also valid syntax
> thing
{cat: 'meow', dog: 'woof'}
> f.cat
'meow'
> f['dog']
'woof'
Here are the basic concepts :
The image at the top of the page outlines the idea, including a collision.
OK, to make this more concrete, let's implement one of these things ourselves. That is, create a hash table without using the built-in dict. To keep things familiar, we'll work in python for now.
Here's a starting template.
def hash_function(key):
""" Given something (usually a string), return an integer """
# TODO
return 1
class HashTable:
def __init__(self):
# TODO
pass
def get(self, key):
""" return corresponding value or None if not found """
# TODO
return None
def insert(self, key, value):
""" put (key,value) into the hash table """
# TODO
pass
def main():
hashtable = HashTable() # create a hash table
hashtable.insert("cat", "meow") # insert some data into it
meow = hashtable.get("cat") # fetch some data from it
print("The cat says ", meow)
main()
The rest we'll do in class.
We had a good discussion of the ideas, and were able to come up with the attached hash table implementation in python ... though the collision algorithm is not yet tested.
last modified | size | ||
hash.py | Mon Mar 28 2022 03:18 pm | 1.5K |