Skip to content

Instantly share code, notes, and snippets.

@haocs
haocs / tmux-cheatsheet.markdown
Created July 24, 2019 17:40 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@haocs
haocs / bs.md
Created November 20, 2017 08:24
bs variants: find left or right boundary
def binary_left(seq, target):
    l, r = 0, len(seq) - 1
    while l < r:
        mid = (l + r) // 2 # always choose the left half
        # if seq[mid] == target:
        #     r = mid
        if seq[mid] >= target:
            r = mid
 else:
@haocs
haocs / isDAG.md
Last active November 20, 2017 08:21
def isDAG(G):
    # status: 1 discover 2 visited
    def has_cycle(G, status, node):
        if status[node] == 2:
            return False
        status[node] = 1
        neighbors = G[node]
        for neighbor in neighbors:
 if status[neighbor] == 1:
@haocs
haocs / nodejs-express-cors.js
Last active September 2, 2016 00:23
Express middle-ware to allow cross origin request
// Allow CORS for client side JS tests
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Methods", 'HEAD,GET,PUT,POST,DELETE,PATCH,OPTIONS');
// Allow all custom headers in reqeusts
res.header("Access-Control-Allow-Headers", req.headers['access-control-request-headers']);
// Specify headers
//res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, x-ms-client-request-id');
// intercept OPTIONS method

Keybase proof

I hereby claim:

  • I am haocs on github.
  • I am haocs (https://keybase.io/haocs) on keybase.
  • I have a public key whose fingerprint is 4D6B F6A5 B482 44A8 2752 D189 978F 38F6 82A1 2F91

To claim this, I am signing this object:

@haocs
haocs / requestpipeline.js
Created May 7, 2016 00:54
A promise based request pipeline demo
var createFilter = function(i) {
return {
before: function(req) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('[before hook] ' + i);
resolve(req);
}, 500);
});
},
@haocs
haocs / System Design.md
Created April 18, 2016 07:00 — forked from vasanthk/System Design.md
System Design Cheatsheet

#System Design Interview Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

##Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
@haocs
haocs / lru_cache.py
Last active April 15, 2016 06:28
An auto-expiring LRU cache implementation
import random
import time
import unittest
__all__=['Cache']
_current_time_ms = lambda: int(round(time.time() * 1000))
"""
@haocs
haocs / latency.markdown
Created April 9, 2016 18:28 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

class RPN():
_input_priority = {
'*': 5,
'/': 5,
'+': 3,
'-': 3,
'(': 7,
}
_instack_priority = {
'*': 6,