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
class UnionFind: | |
def __init__(self): | |
self.parent = {} | |
def find(self, x): | |
while x != self.parent.setdefault(x, x): | |
x = self.parent[x] = self.parent[self.parent[x]] | |
return x | |
def unite(self, x, y): |
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
from typing import Generator, Tuple | |
def diagonal_traversal(width: int, height: int) -> Generator[Tuple[int, int], None, None]: | |
"""Prints coordinates of a rectangle in diagonal/zipzag order""" | |
for k in range(1, width + height): | |
i_range = reversed(range(0, min(width, k))) | |
j_range = range(max(k - width, 0), height) | |
yield from zip(i_range, j_range) |
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
use std::pin::Pin; | |
#[derive(Debug)] | |
pub struct SuffixArray<'a> { | |
_string: Pin<String>, | |
array: Vec<&'a str>, | |
} | |
impl<'a> SuffixArray<'a> { | |
pub fn new(string: impl Into<String>) -> Self { |
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
/** Generates a flat `Object` of all key paths and values of a nested object */ | |
const flattenObject = (object, delimiter = ".") => { | |
const queue = Object.entries(object); | |
const flatObject = {}; | |
while (queue.length) { | |
const [parentKey, parentValue] = queue.shift(); | |
if (typeof parentValue !== "object") { | |
flatObject[parentKey] = parentValue; | |
} else { | |
for (const childKey in parentValue) { |
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
/** Used as split for Huffman Tree tokens */ | |
const segmenter = new Intl.Segmenter("en", { granularity: "word" }); | |
/** Node of a Huffman Tree */ | |
type Node = [Node, Node] | string; | |
/** Returns the frequency of `Node`s */ | |
function getCounts(tokens: string[]): Map<Node, number> { | |
const counts: Map<Node, number> = new Map(); | |
for (const token of tokens) { |
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
/** Based on original Penn Treebank tokenization method */ | |
export const tokenize = (s: string): string[] => { | |
// # attempt to get correct directional quotes | |
// s=^"=`` =g | |
// s=\([ ([{<]\)"=\1 `` =g | |
// # close quotes handled at end | |
s = s.replace(/^"/, "`` "); | |
s = s.replace(/([ ([{<])"/g, "$1 `` "); | |
// s=\.\.\.= ... =g |
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
/** Two or more word characters surrounded by word breaks */ | |
const TOKEN_PATTERN = /\b\w\w+\b/gu; | |
/** Lowercase words from a text */ | |
const tokenize = (text) => { | |
return text.match(TOKEN_PATTERN)?.map((token) => token.toLowerCase()); | |
}; | |
/** Jaccard similarity measure for two texts */ | |
const jaccardSimilarity = (textI, textJ) => { |
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
/** Yields `r` length `Arrays` of integers between `0` and `n` that sum to n. */ | |
export function* allocation(r: number, n: number): Generator<number[]> { | |
if (r === 0) { | |
if (n === 0) { | |
yield []; | |
} | |
return; | |
} | |
const indices = Array(r - 1).fill(0); | |
indices[r - 1] = n; |
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Calculate the fibonacci number at index *n* using Cassini's identity and | |
memoization. | |
Example: | |
>>> fibonacci(10) | |
89 |
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Infinite iterable of digits of pi using the spigot algorithm. | |
Example: | |
>>> pi = pi_spigot() | |
>>> next(pi) | |
3 |
NewerOlder