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
// ported from: http://stackoverflow.com/questions/2140787/select-random-k-elements-from-a-list-whose-elements-have-weights | |
// each node in the heap has a value, weight, and totalWeight | |
// the totalWeight is the weight of the node plus any children | |
var Node = {}; | |
var newNode = function (value, weight, totalWeight) { | |
var node = Object.create(Node); | |
node.value = value; | |
node.weight = weight; | |
node.totalWeight = totalWeight; |
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
/* powers of 2 utility view for heap traversal */ | |
CREATE VIEW IF NOT EXISTS power2 AS | |
WITH RECURSIVE doubling(n) AS ( | |
VALUES (2) | |
UNION ALL | |
SELECT n*2 FROM doubling LIMIT 62 | |
) | |
SELECT n twos FROM doubling; | |
/* generates random floating point values in interval [0, 1) */ |
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
# coding=utf-8 | |
from collections import Counter, defaultdict | |
from functools import reduce | |
from itertools import tee | |
import re | |
LOW_PROBABILITY = 1.0 / (1 << 20) | |
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
intervaltree | |
* storing timelines or other intervals | |
* sorting will not work, you have to fully search | |
* introducing interval trees, which allow O(log n) access and modification | |
* interval trees of interval trees for multi-dimensional data... most basic spatial data stuff | |
* intervaltree on pypi | |
fancy pythonic dijkstra | |
* finding shortest paths in a graph | |
* using heaps and making backpathing trees with tuples |
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
# coding=utf-8 | |
from collections import OrderedDict, namedtuple | |
from functools import total_ordering | |
from heapq import heappush, heappop | |
from itertools import count, zip_longest | |
INITIAL_START = object() | |
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 minimum(stack): | |
gotten = [] | |
try: | |
while True: | |
gotten.append(stack.pop()) | |
except ValueError: | |
pass | |
if not gotten: | |
return 0 |
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
# coding=utf-8 | |
from __future__ import unicode_literals | |
def check_parens(string, pairs="()"): | |
""" | |
Check a string for non-matching braces or other character pairs and return | |
a list of failures, or an empty set if everything is OK. | |
`if check_parens(string, brackets):` is a good way to find bad brackets in |
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
# coding=utf-8 | |
from datetime import datetime | |
from functools import partial | |
from itertools import chain, groupby | |
from operator import itemgetter | |
import os | |
import re | |
import sys | |
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
# coding=utf-8 | |
from __future__ import unicode_literals | |
from datetime import datetime | |
DATE_FORMAT = "%m/%d" | |
def convert_date(date_str): |
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
# coding=utf-8 | |
import re | |
def simple_compress(s): | |
c = ''.join((part[1] + (str(len(part[0])) if len(part[0]) > 1 else '')) for part in re.findall(r'((.)\2*)', s)) | |
return c if len(c) < len(s) else s |
OlderNewer