Skip to content

Instantly share code, notes, and snippets.

View N8Brooks's full-sized avatar

skookumchoocher N8Brooks

  • Sioux Falls, South Dakota
View GitHub Profile
@N8Brooks
N8Brooks / somewhat_self_compressing_union_find.py
Created February 24, 2024 01:13
Somewhat Self-Compressing Union-Find
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):
@N8Brooks
N8Brooks / diagonal_traversal.py
Created August 25, 2023 15:36
Diagonal Traversal
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)
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 {
/** 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) {
/** 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) {
/** 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
@N8Brooks
N8Brooks / jaccard_similarity.js
Created February 18, 2022 23:38
Jaccard similarity measure for two texts
/** 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) => {
@N8Brooks
N8Brooks / allocation.ts
Created October 12, 2021 03:34
Yields `r` length `Arrays` of integers between `0` and `n` that sum to n.
/** 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;
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Calculate the fibonacci number at index *n* using Cassini's identity and
memoization.
Example:
>>> fibonacci(10)
89
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Infinite iterable of digits of pi using the spigot algorithm.
Example:
>>> pi = pi_spigot()
>>> next(pi)
3