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
def _hash_str(self, string): | |
"""hash_str uses the djb2 algorithm to compute the hash | |
value of a string http://www.cse.yorku.ca/~oz/hash.html""" | |
hash = 5381 | |
for char in string[1:]: | |
# (hash << 5) + hash is equivalent to hash * 33 | |
hash = (hash << 5) + hash + ord(char) | |
return hash |
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 Node(object): | |
def __init__(self, data): | |
"""Initialize this node with specified data""" | |
self.data = data | |
self.next = None | |
class LinkedList(object): | |
def __init__(self, items=None): |
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
from linked_list import LinkedList | |
class HashTable(object): | |
def __init__(self, items = None): | |
"""Initialize this HashTable and set items if specified""" | |
self.slots = [LinkedList() for i in range(8)] # start with 8 slots | |
self.size = 0 |
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
from linked_list import LinkedList | |
class HashTable(object): | |
def __init__(self, items = None): | |
"""Initialize this HashTable and set items if specified""" | |
self.slots = [LinkedList() for i in range(8)] # start with 8 slots | |
self.size = 0 |
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
def get_items(self): | |
"""Return a list of tuples representing all the items in the HashTable""" | |
return [items.extend(slot.items()) for slot in self.slots] | |
def _resize(self): | |
""""Resize the HashTable by doubling the number of slots and rehashing all items""" | |
# get a list of all items in the hash table | |
items = self.get_items() | |
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
def contains(self, key): | |
"""Return True if the key is found in the HashTable, | |
otherwise return False""" | |
# get the slot (linked_list) the key belongs to | |
# using our _get_hash_index function | |
slot = self.slots[self._get_hash_index(key)] | |
# look for key in linked list | |
# if found return True, otherwise return False |
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 CircularBuffer(object): | |
def __init__(self, max_size=10): | |
"""Initialize the CircularBuffer with a max_size if set, otherwise | |
max_size will default to 10""" | |
self.buffer = [None] * max_size | |
self.head = 0 | |
self.tail = 0 | |
self.max_size = max_size |
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
def size(self): | |
"""Return the size of the CircularBuffer | |
Runtime: O(1) Space: O(1)""" | |
if self.tail >= self.head: | |
return self.tail - self.head | |
return self.max_size - self.head - self.tail | |
def is_empty(self): | |
"""Return True if the head of the CircularBuffer is equal to the tail, | |
otherwise return False |
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
def enqueue(self, item): | |
"""Insert an item at the back of the CircularBuffer | |
Runtime: O(1) Space: O(1)""" | |
if self.is_full(): | |
raise OverflowError( | |
"CircularBuffer is full, unable to enqueue item") | |
self.buffer[self.tail] = item | |
self.tail = (self.tail + 1) % self.max_size | |
def dequeue(self): |
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
#!python | |
class CircularBuffer(object): | |
def __init__(self, max_size=10): | |
"""Initialize the CircularBuffer with a max_size if set, otherwise | |
max_size will elementsdefault to 10""" | |
self.buffer = [None] * max_size | |
self.head = 0 |
OlderNewer