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 / Context.js
Last active June 20, 2019 17:44
Context API Component
import React, { useState } from 'react';
export const Context = React.createContext(null);
export const ContextProvider = ({ initialState, children }) => {
const [state, setState] = useState(initialState);
const providerActions = {
setState: values =>
setState(prevState => {
@edwintcloud
edwintcloud / circular_buffer.cpp
Created May 7, 2019 18:40
Circular Buffer in C++
//===================================================================
// File: circular_buffer.cpp
//
// Desc: A Circular Buffer implementation in C++.
//
// Copyright © 2019 Edwin Cloud. All rights reserved.
//
//===================================================================
//-------------------------------------------------------------------
@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
@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
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 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 / 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 / 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: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
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