This file contains hidden or 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 | |
class NaryHeap(object): | |
"""implements an n-ary heap""" | |
def __init__(self, items=(), *, n=2, direction=min): | |
""" | |
create a new heap |
This file contains hidden or 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 math import log2, ceil | |
# valid chars for a url path component: a-z A-Z 0-9 .-_~!$&'()*+,;=:@ | |
# for the default set here (base 72) we have excluded $'();:@ | |
radix_alphabet = sorted( | |
"0123456789" | |
"abcdefghijklmnopqrstuvwxyz" | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
This file contains hidden or 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 | |
try: | |
# noinspection PyUnresolvedReferences,PyShadowingBuiltins | |
str = unicode | |
except NameError: | |
# noinspection PyShadowingBuiltins,PyUnboundLocalVariable | |
str = str |
This file contains hidden or 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 itertools import product | |
adjacents = ('08', '124', '1253', '236', '1457', '24568', '3569', '478', '57890', '689') | |
adjacents = {str(i): s for i, s in enumerate(adjacents)} | |
def get_pins_itertools(observed): | |
for pins in product(*(adjacents[ch] for ch in observed)): |
This file contains hidden or 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 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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() | |