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
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)]
@mesejo
mesejo / reservoir_sampling.py
Created May 13, 2021 20:58
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)
def my_zip(*iterables):
iters = [iter(i) for i in iterables]
while True:
try:
yield tuple([next(it) for it in iters])
except StopIteration:
return
"""
A module for when you find modern numeric notation far too convenient to use
and want to go back to good old fashioned roman numerals.
You can import any numeral you like from here and it will work just fine.
e.g.
>>> from numerals import IX, XVIIIII
>>> IX + XVIIIII
XXIX
@jatinsharrma
jatinsharrma / BranchSumBT.py
Created April 3, 2020 12:39
BInary Tree branch sum
'''
Write a function that takes in a binary tree and returns a list of its branch sums
(ordered from the sum of the left branch to the sum of the branch at right).
A branch sum is the sum of all values within a branch of the Binary Tree.
A branch of a Binary Tree is a path in a tree that begins at the root node and ends at any leaf node.
'''
#This class makes a node
class Node:
def __init__(self,value):
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
try fetchedResultsController.performFetch()
} catch {
print("Error")
}
}
@simecek
simecek / reprlibdemo.ipynb
Created November 4, 2018 22:13
ReprlibDemo.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@georgeth
georgeth / recover_source_code.md
Created August 8, 2018 14:44 — forked from simonw/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
@rajatdiptabiswas
rajatdiptabiswas / Binary Indexed Tree.py
Last active June 22, 2023 05:20
Implementation of Binary Indexed Tree/Fenwick Tree in Python
#!/usr/bin/env python3
"""
Binary Indexed Tree / Fenwick Tree
https://www.hackerearth.com/practice/notes/binary-indexed-tree-made-easy-2/
https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/
https://www.youtube.com/watch?v=v_wj_mOAlig
https://www.youtube.com/watch?v=kPaJfAUwViY
"""
@folksilva
folksilva / problem1.py
Last active February 28, 2022 13:39
Daily Coding Problem: Problem #1
"""
This problem was asked by Google.
Given a stack of N elements, interleave the first half of the stack with the second half reversed using only one other queue. This should be done in-place.
Recall that you can only push or pop from a stack, and enqueue or dequeue from a queue.
For example, if the stack is [1, 2, 3, 4, 5], it should become [1, 5, 2, 4, 3]. If the stack is [1, 2, 3, 4], it should become [1, 4, 2, 3].
Hint: Try working backwords from the end state.