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
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 => { |
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
//=================================================================== | |
// File: circular_buffer.cpp | |
// | |
// Desc: A Circular Buffer implementation in C++. | |
// | |
// Copyright © 2019 Edwin Cloud. All rights reserved. | |
// | |
//=================================================================== | |
//------------------------------------------------------------------- |
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 |
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
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
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 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
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
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 |
NewerOlder