Skip to content

Instantly share code, notes, and snippets.

View danielhao5's full-sized avatar
💭
Learn new things everyday.

Daniel Hao danielhao5

💭
Learn new things everyday.
View GitHub Profile
@danielhao5
danielhao5 / recover_source_code.md
Created February 13, 2020 18:00 — forked from georgeth/recover_source_code.md
How to recover lost Python source code if it's still resident in-memory

How to recover lost Python source code if it's still resident in-memory

I screwed up using git ("git checkout --" on the wrong file) and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

Attach a shell to the docker container

Install GDB (needed by pyrasite)

apt-get update && apt-get install gdb
@danielhao5
danielhao5 / uf.py
Created November 6, 2020 22:52 — forked from kachayev/uf.py
Union-Find data structure implementation with examples (Kruskala MST, Tarjan LCA etc)
# Union-Find (Disjoint Set Union)
# more information on wiki:
# http://en.wikipedia.org/wiki/Disjoint-set_data_structure
class UF(object):
def __init__(self, size):
self.p = [None]*size
self.rank = [1]*size
import random
import perfplot
from collections import defaultdict, Counter
from itertools import chain
from pyroaring import BitMap
def setup(k):
lss = [f"list{i}" for i in range(1, k + 1)]
labels = [f"label{i}" for i in range(1, 101)]
@danielhao5
danielhao5 / reservoir_sampling.py
Created October 9, 2021 16:47 — forked from mesejo/reservoir_sampling.py
Implementation of the L algorithm for reservoir sampling
import random
import numpy as np
from numpy.random import default_rng
from itertools import islice
def reservoir_sampling(pop, k):
reservoir = []
stream = iter(pop)