Skip to content

Instantly share code, notes, and snippets.

View cabiad's full-sized avatar

Chris Abiad cabiad

View GitHub Profile
@tomhicks
tomhicks / useTaskQueue.ts
Created January 11, 2021 11:41
React Hook for queueing and processing async tasks sequentially
function useTaskQueue(params: {
shouldProcess: boolean
}): {
tasks: ReadonlyArray<Task>
isProcessing: boolean
addTask: (task: Task) => void
} {
const [queue, setQueue] = React.useState<{
isProcessing: boolean
tasks: Array<Task>
@cabiad
cabiad / find_missing_value.py
Created February 28, 2013 19:49
Solution to following problem: Array A contains all integers from 0 to n, except one is missing. In this problem we cannot access an entire integer in A in a single op. The elements of A are represented in binary, and the only op we can use is “fetch the jth bit of A[i],” which takes constant time. Write code to find the missing integer. Can you…
####################################
# Copyright Christopher Abiad, 2013
# All Rights Reserved
####################################
__author__ = 'Christopher Abiad'
def find_missing_one(bit_index, A):
# there is only one item
if bit_index == 0:
@cabiad
cabiad / qsort.py
Created February 18, 2013 02:02
A simple and fun experiment in generating a Python2 quicksort method that performs the sorting in-place. Plenty of comments and a few tests at the bottom might actually make this code helpful for someone studying Python and the quicksort algorithm. See the following excellent resources for more info on quick-sort: http://www.sorting-algorithms.c…
####################################
# Copyright Christopher Abiad, 2013
# All Rights Reserved
####################################
"""In-place quicksorting."""
__author__ = 'Christopher Abiad'
from random import randrange
@cabiad
cabiad / heap.py
Last active December 13, 2015 20:19
A simple Python2 binary min-heap with a simple heapsort implementation. Also, some fuzz testing and a bunch of unit tests for the heap and its helper methods.
####################################
# Copyright Christopher Abiad, 2013
# All Rights Reserved
####################################
"""Binary min-heap with a simple heapsort implementation.
Heap is stored entirely in an array (Python list) using the property that
for any node at position i, its two children can be stored at 2i and
2i + 1 without any collisions with other nodes.
"""
@cabiad
cabiad / mergesort.py
Created February 10, 2013 00:41
Quick code kata, a merge sort in Python. Completed first on whiteboard, manually tested there, then re-tested by typing and running it through the interpreter.
####################################
# Copyright Christopher Abiad, 2012
# All Rights Reserved
####################################
__author__ = 'Christopher Abiad'
def mergesort(l):
n = len(l)
if n <= 1: