Skip to content

Instantly share code, notes, and snippets.

View tonyslowdown's full-sized avatar

Joseph tonyslowdown

  • San Francisco Bay Area
View GitHub Profile
@tonyslowdown
tonyslowdown / spacemacs-cheatsheet.md
Created February 25, 2018 22:30 — forked from davoclavo/spacemacs-cheatsheet.md
Spacemacs cheatsheet

emacs --daemon to run in the background. emacsclient.emacs24 <filename/dirname> to open in terminal

NOTE: "M-m and SPC can be used interchangeably".

  • Undo - C-/
  • Redo - C-?
  • Change case: 1. Camel Case : M-c 2. Upper Case : M-u
  1. Lower Case : M-l
@tonyslowdown
tonyslowdown / combinations.scala
Last active June 3, 2017 19:17
Scala combinations
def comb[T](l: List[T]): List[List[T]] = l match {
case Nil => List(Nil)
case x::xs => {
val prevResult = comb(xs)
prevResult ++ prevResult.map(y => x::y)
}
}
@tonyslowdown
tonyslowdown / lru_cache.py
Created December 14, 2016 23:10
LRU Cache
"""
LRUCache
"""
class LRUCache:
"""
Least Recently Used Cache
Implemented with a dictionary and a linked list
"""
def __init__(self, capacity):
@tonyslowdown
tonyslowdown / threading_stuff.py
Last active November 21, 2016 00:16
Examples of threading in python taken from slides (http://www.dabeaz.com/usenix2009/concurrent/Concurrent.pdf) that @dabeaz presented at USENIX Technical Conference, June 15, 2009 and other resources
import time
import threading
"""
Threads defined by a class
"""
class CountdownThread(threading.Thread):
def __init__(self,count):
threading.Thread.__init__(self)
class TrieNode:
def __init__(self):
"""Node objects within trie
"""
self.children = {}
self.is_word = False
class Trie:
@tonyslowdown
tonyslowdown / optimized_trie.py
Created November 1, 2016 02:37
Trie object in python using hash tables for keeping track of children
class Trie:
"""
Trie object will try to optimize by keeping unseen suffixes in a single node,
and then unravels those suffixes when the letters in the suffix are encountered
"""
class Node:
"""Each trie node"""
def __init__(self):
self.children = {}
self.count = 1
@tonyslowdown
tonyslowdown / minheap.py
Created October 29, 2016 23:23
MinHeap (or minPQ) in python that allows updates to items already in the heap
class MinHeap:
"""Min Heap
"""
def __init__(self):
self.N = 0
self.vals = []
self.val2pos = {}
self.pos2key = {}
@property
import Text.Printf
import Control.Exception
import System.CPUTime
time :: IO t -> IO t
time a = do
start <- getCPUTime
v <- a
end <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
# Edit distance problem
class Solution(object):
def minDistance_recursive(self, word1, word2):
"""
Recursive solution
:type word1: str
:type word2: str
:rtype: int
'''
Sieve is good for pre-generating list of primes, so that you can check quickly if a number is prime or not.
If you're checking if a single number is a prime or not, use the looping method
'''
import math
def generate_primes_sieve(n):
non_primes = set()