This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def range_sum(tree, left_child, right_child): | |
return tree[left_child] + tree[right_child] | |
def build_segment_tree(arr, tree=None, | |
merge_fn=range_sum, | |
node_index=0, lo=0, hi=None, initial=None): | |
""" | |
Build a segment tree based on arr of leaf node | |
tree will have the size of 4 * len(arr) | |
node_index is tree index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://code.activestate.com/recipes/576930-efficient-running-median-using-an-indexable-skipli/ | |
# Updated to run with python | |
from random import random | |
from math import log, ceil | |
class Node(object): | |
__slots__ = 'value', 'next', 'width' | |
def __init__(self, value, next, width): | |
self.value, self.next, self.width = value, next, width |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DisjointSet(object): | |
roots = None | |
ranks = None | |
count = 0 | |
def __init__(self, size): | |
# Each node is parent of itself | |
self.roots = list(range(size)) | |
self.ranks = [0] * size | |
self.count = size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
import random | |
base_case = [ | |
[(0, 1), (1, 1), (1, 0)], # 0, Top Left | |
[(0, 0), (1, 0), (1, 1)], # 1, Top Right | |
[(0, 0), (0, 1), (1, 1)], # 2, Bottom Left | |
[(0, 1), (0, 0), (1, 0)] # 3, Bottom Right | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Least frequency cache implementation using nested DoublyLinkedList and dictionary | |
Use bucket of frequency count B[1] contain all Item(key, value) with frequency 1 | |
DEMO ONLY do not use in prod | |
Example | |
B[1] B[5] B[6] | |
I[1, 1] I[4, 4] I[5, 5] | |
I[2, 2] | |
I[3, 3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rotate(mat: List, clockwise: bool=True) -> None: | |
""" Rotate matrix inplace but look like it still use extra memory. run on C """ | |
if clockwise: | |
mat[:] = map(list, zip(*mat[::-1])) | |
else: | |
mat[:] = map(list, list(zip(*mat))[::-1]) | |
return mat | |
def rotate(mat: List, clockwise: bool = True) -> None: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor | |
def task_runner(opts): | |
if isinstance(opts, (list, tuple)): | |
num_opts = len(opts) | |
args = None | |
kwargs = None | |
if num_opts == 2: | |
fn, args = opts | |
elif num_opts == 3: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from cStringIO import StringIO | |
class Heap(object): | |
''' index from parent | |
0 0 | |
1 2 0*2+1=1 0*2+2=2 | |
3 4 5 6 1*2+1=3 1*2+2=4 2*2+1=5 2*2+1=6 | |
7 8 9 10 11 12 13 14 3*2+1=7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.getCollection('article').aggregate([ | |
{$match: {$or: [ | |
{"view.sections.parts.embed.provider_name": {$in: ['Commerce Product', 'Image With Hotspots', 'Commerce Product Group']}}, | |
{"view.sections.steps.parts.embed.provider_name": {$in: ['Commerce Product', 'Image With Hotspots', 'Commerce Product Group']}} | |
]}}, | |
{$project: {_id: 1} | |
]) | |
db.getCollection('article').aggregate([ | |
{$match: {$or: [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function csvBlob(rows, export_bom) { | |
const BOM = '\ufeff'; // Utf-8 Bytes order mark | |
return new Blob([(export_bom ? BOM : '') + rows.map(function(row) { | |
return row.map(function(col) { | |
return ['"', col.replace(/"/gi, '""'), '"'].join(''); | |
}).join(',') | |
}).join('\n')], {type: 'text/csv;charset=utf-8;'}); | |
} | |
const blob = csvBlob(temp1); |