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
"""Multiset implementation. | |
""" | |
__all__ = ["Multiset"] | |
from sortedcontainers import SortedList # https://pypi.org/project/sortedcontainers/ | |
from collections import Counter, deque |
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 joint_groupby(*iterables, key=None, ordered=True): | |
"""Like itertools.groupby, but iterates through a collection of iterables. | |
If the value type (or the return type of key) is not comparable, you MUST pass ordered=False. | |
Example: [(x, tuple(sum(1 for _ in group) for group in groups)) for x, groups in joint_groupby(...)]""" | |
if key is None: | |
key = lambda x: x | |
iterables = [iter(it) for it in iterables] | |
stop = object() | |
buffer = [next(it, stop) for it in iterables] |
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 xgcd(a, b): | |
"https://anh.cs.luc.edu/331/notes/xgcd.pdf" | |
x, x_ = 1, 0 | |
y, y_ = 0, 1 | |
while b: | |
q, b, a = *divmod(a, b), b | |
x, x_ = x_, x - q*x_ | |
y, y_ = y_, y - q*y_ | |
#assert a0*x + b0*y == math.gcd(a0, b0) | |
return x, y |
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 _ez_generator_interface(g): | |
"""Handy wrapper for generator-driven program flows. | |
Example usage: | |
def g(questions=[("What's 3 + 3?", 6), ("What's 9 + 10?", 19)]): | |
"Example generator function that yields queries expecting a reply" | |
for question, correct_answer in questions: | |
answer = yield question # <- KEY LINE | |
if int(answer) != correct_answer: |
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 email.message, email.parser, email.policy | |
import urllib.parse | |
import re | |
from collections import namedtuple | |
try: | |
from resource import getpagesize | |
except ImportError: | |
import mmap | |
def getpagesize(): | |
return mmap.PAGESIZE |
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 last_qualified(predicate, iterable, /, return_if_exhausted=True): | |
""" | |
Consumes iterable or sequence `iterable` until finding an item | |
not satisfying `predicate`, or until exhausted, then returns the last item | |
which satisfied `predicate`. | |
Raises ValueError if `iterable` is empty. | |
Raises ValueError if the first item from `iterable` does not satisfy `predicate`. | |
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 functools | |
from .. import NORMALIZE_FUNC, EXPENSIVE_API_CALL | |
@(lambda t: lambda f: functools.wraps(f)(lambda x: f(t(x))))(NORMALIZE_FUNC) | |
@functools.lru_cache | |
def f(x): | |
return EXPENSIVE_API_CALL(x) |
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
// example: createXMLDocumentWithDeclaration('http://www.topografix.com/GPX/1/1', 'gpx', null, 'version="1.0" standalone="yes"') | |
function createXMLDocumentWithDeclaration(namespaceURI, qualifiedNameStr, documentType=null, declarationData='version="1.0"') { | |
const document = new Document().implementation.createDocument(namespaceURI, qualifiedNameStr, documentType); | |
// https://www.w3.org/TR/xml/#NT-XMLDecl | |
document.insertBefore(document.createProcessingInstruction("xml", declarationData), document.firstChild); | |
return document; | |
} |
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 operator import eq as _eq | |
from itertools import starmap as _starmap, chain as _chain, repeat as _repeat | |
def _istartswith(a, b): | |
""" | |
Returns True if every element of b is equal to the zip-corresponding element of a; | |
returns False otherwise, including in the event that a has fewer elements than b. | |
""" | |
return all(_starmap(_eq, zip(_chain(a, _starmap(object, _repeat(()))), b))) |
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 operator import xor as _xor | |
from functools import reduce as _reduce | |
# James Edington <james.edington@uah.edu> | |
# h/t https://wunkolo.github.io/post/2020/05/pclmulqdq-tricks/ | |
# h/t https://en.wikipedia.org/wiki/Carry-less_product | |
def clmul(a: int, b: int) -> int: | |
partial_products = ( b*(a & 1<<k) for k in range(a.bit_length()) ) | |
return _reduce(_xor, partial_products, 0) |