-
Notifications
You must be signed in to change notification settings - Fork 1
Hash table #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hash table #6
Conversation
…o bst Merging branch bst.
…o bst sdf building a binary search tree.
…ctures into traversal-bst g g Lines starting with '#' will be ignocquire post_order_trav. red, and an empty message aborts
…ures into balance-bst ng tests for left and right rotations.#
…ures into balance-bst merging branch.
… branch 'balance-bst' of https://github.com/chamberi/data-structures into balance-bst
| * g.breadth_first_travers(start): Returns the path list for the entire graph with a breadth first traversal. | ||
| - get(key) - should return the value stored with the given key | ||
| - set(key, val) - should store the given val using the given key | ||
| - _hash(key) - should hash the key provided (note that this is an internal api) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thorough documentation. Nice.
|
|
||
| def __init__(self, hash_type='additive'): | ||
| """Init function for the Hash Table class.""" | ||
| self._num_buckets = 50000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many buckets! :)
| """Init function for the Hash Table class.""" | ||
| self._num_buckets = 50000 | ||
| self._container = [[] for i in range(0, self._num_buckets)] | ||
| self._type = hash_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider what you want to happen if a user inputs an invalid hash type.
| for each in self._container[hashed_value]: | ||
| if each[0] == key: | ||
| return each[1] | ||
| return 'Key not in hash table.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very pythonic returns, but do you really want to be returning a string? Would it make more sense to raise an error?
| return self._colin_ben_hash(key) | ||
|
|
||
| def _additive_hash(self, key): | ||
| return sum([ord(each) for each in key]) % self._num_buckets |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this one liner
| def _additive_hash(self, key): | ||
| return sum([ord(each) for each in key]) % self._num_buckets | ||
|
|
||
| def _colin_ben_hash(self, key): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah. I like the name, make sure to document this hash function!
| # num = int(str(ord(each))[::-1]) | ||
| # ords.append([num, next(sieve)]) | ||
| # a_sum = sum([(each[0] << next(sieve)) * (each[1] << next(sieve)) for each in ords]) | ||
| # return a_sum % self._num_buckets |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to remove corpse code.
| def test_table_correct(colin_ben_filled_hash_table): | ||
| """Testing that get works correctly.""" | ||
| count = 0 | ||
| for line in open('/usr/share/dict/words'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where are you closing this file?
| def test_table_correct2(additive_filled_hash_table): | ||
| """Testing that get works correctly.""" | ||
| # import pdb; pdb.set_trace() | ||
| for line in open('/usr/share/dict/words'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you seem to be opening this dictionary a lot...is there a way to speed this up and reuse one data structure?
No description provided.