Skip to content

Instantly share code, notes, and snippets.

View edwintcloud's full-sized avatar
🎯
Focusing

Edwin Cloud edwintcloud

🎯
Focusing
View GitHub Profile
@edwintcloud
edwintcloud / string_hash.py
Last active April 20, 2019 19:21
djb2 string hashing
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
@edwintcloud
edwintcloud / linked_list.py
Last active April 23, 2019 17:56
Linked List Implementation in Python
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):
@edwintcloud
edwintcloud / hash_table.py
Last active March 10, 2021 09:58
Hash Table Implementation in Python
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
@edwintcloud
edwintcloud / hash_table.py
Created April 29, 2019 18:21
Hash Table Part 1
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
@edwintcloud
edwintcloud / hash_table.py
Created April 29, 2019 18:23
Hash Table Part 2
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()
@edwintcloud
edwintcloud / hash_table.py
Created April 29, 2019 18:24
Hash Table Part 3
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
@edwintcloud
edwintcloud / circular_buffer.py
Created May 7, 2019 17:48
Circular Buffer in Python Part 1
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
@edwintcloud
edwintcloud / circular_buffer.py
Last active May 7, 2019 17:54
Circular Buffer in Python Part 2
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
@edwintcloud
edwintcloud / circular_buffer.py
Created May 7, 2019 18:07
Circular Buffer in Python Part 3
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):
@edwintcloud
edwintcloud / circular_buffer.py
Created May 7, 2019 18:16
Circular Buffer in Python Full Implementation
#!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